Hi,
I' having some problems with binding data to a checkbox in a WebDataGrid.
I tried a few things myself, and for some reason I just cant get it to work, Basicly i'm looking for something like:
preview of my WebDataGrid: http://img41.imageshack.us/f/permissionsb.png/
I got Users, wich on check get permissions, so when I check an checkbox for a user in his row, OnClick add permission I want to add permission by wich loopin through the WebDataGrid and all checkboxes wich are checked need to be added.
If any1 could explain how to do this, or show me an example
I'd really appriciate it
Dim cb As CheckBox Dim dgi As DataGridItem Dim idList As String = ""
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click For Each dgi In WebDataGrid1.Items cb = CType(dgi.Cells(CB_Algemeen).Controls(1), CheckBox) If cb.Checked Then Dim permissionController As New Permissions.PermissionController() Dim chkList As CheckBoxList = DirectCast(sender, CheckBoxList) chkList.DataSource = permissionController.GetPermissionByCodeAndKey("SYSTEM_TAB", "VIEW") chkList.DataTextField = "PermissionName" chkList.DataValueField = "PermissionID" chkList.DataBind() End If Next End Sub
<ig:WebDataGrid ID="WebDataGrid1" runat="server" EnableAjax="False" Height="350px" Width="637px" AutoGenerateColumns="False"> <Columns> <ig:BoundDataField DataFieldName="UserID" Key="UserID" Hidden="True"> <Header Text="UserID" /> </ig:BoundDataField> <ig:BoundDataField DataFieldName="Username" Key="Username" Width="90px"> <Header Text="Username" /> </ig:BoundDataField> <ig:TemplateDataField Key="Algemeen" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Algemeen" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Algemeen"></asp:Label> <input type="checkbox" name="SelectAlgemeen" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="Financieel" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Financieel" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Financieel"></asp:Label> <input type="checkbox" name="SelectFinancieel" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="Licentie" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Licentie" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Licentie"></asp:Label> <input type="checkbox" name="SelectLicentie" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="Applicatie" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Applicatie" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Applicatie"></asp:Label> <input type="checkbox" name="SelectApplicatie" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="UitDienst" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="UitDienst" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Uit Dienst"></asp:Label> <input type="checkbox" name="SelectUitDienst" onclick="Check(this);" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> </Columns></ig:WebDataGrid>
Visual Basic Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim userInfo As UserInfo userInfo = UserController.GetCurrentUserInfo() If userInfo.UserID = -1 Then End If Company = userInfo.Profile.GetPropertyValue("Company") Dim strSQLconnection As String = ("Data Source=.\SQLExpress;Initial Catalog=dnn2;User ID=vb1;Password=Klompsrv1") cSQL = "SELECT Users.UserID, Users.Username, UserProfile.PropertyValue FROM ProfilePropertyDefinition INNER JOIN UserProfile ON ProfilePropertyDefinition.PropertyDefinitionID = UserProfile.PropertyDefinitionID INNER JOIN Users ON UserProfile.UserID = Users.UserID WHERE (ProfilePropertyDefinition.PropertyName = 'Company') AND (UserProfile.PropertyValue = '" + Company + "')" Dim sqlConnection As New SqlConnection(strSQLconnection) Dim sqlCommand As New SqlCommand(cSQL, sqlConnection) Dim SqlAdapter As New SqlDataAdapter(sqlCommand) Dim DataSet As New DataSet() SqlAdapter.Fill(DataSet) If sqlConnection.State = ConnectionState.Closed Then sqlConnection.Open() End If WebDataGrid1.DataSource = DataSet WebDataGrid1.DataBind() End Sub
With an ASP checkbox in your markup, you can use the following code to access the value of those checkboxes.
DirectCast(WebDataGrid.Rows(rowindex).Items(columnindex).FindControl("Checkbox1"),Checkbox).Checked
You can loop through the rows and increment rowindex or you can use a for each.
Ed
how would it be for a username then?
I trierd DirectCast(WebDataGrid.Rows(rowindex).Items(columnindex).FindControl("Username").Value()
but that doesnt work
Crea
That's true, you can cast something that's not there. Your if statement should be if x=(the column index of your checkbox) then cast...
You're also right that this is not the easiest way to work with checkboxes in a grid. Infragistics is coming out with a checkbox provider for this grid in their first release of 2011. Then, it will work smoothly. Until then, you have to work with one of these workarounds.
Hi Ed,
isnt there like a foreach statement I could use, like For Eac Checkbox.item.checked row/column (x),or is the if statement the most efficient way of doing a loop?
Was wondering since I binded the webdatagrid to a dataset is it possible to loop through the dataset, since it has to store the values anyway.
Sebastian
I don't know why you need the whole second loop all together. Whynot do it like this assuming your checkbox is column index 7:
For i As Integer = 0 To WebDataGrid1.Rows.Count - 1 If DirectCast(WebDataGrid1.Rows(i).Items(7).FindControl("Algemeen"), CheckBox).Checked() AndAlso DirectCast(WebDataGrid1.Rows(i).Items.Item(1).Value, objusername) Then Dim User As System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User If Not User.IsInRole("Algemeen") Then Dim rc As New DotNetNuke.Security.Roles.RoleController() Dim ri As DotNetNuke.Security.Roles.RoleInfo = rc.GetRoleByName(PortalId, "Algemeen") Dim ui As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo() rc.AddUserRole(PortalId, ui.UserID, ri.RoleID, Null.Null End If
Next
Hi ed,the wierd thing about it is is return an error 'oObject reference not set to an instance of an object'
I think it has to do with the first line (if DirectCAst(WebDataGrid1.Rows(i).Items(6).FindControl("Algemeen"), Checkbox).Checked()
my thought it is cant find the control wich I cant really understand.
Got any ideas on how to fix this problem, since i've been searching the web for it and I cant find anything about it.
In your markup, Algemeen checkbox is in the 3rd column so it shouldn't be items(6), it should be items(2). Findcontrol finds a control in the template and there is no checkbox control in column 7.
Hi ed,
Since i'm working with DotNetNuke, and i'm creating my own modules for it. I found something wich does almost the exact same thing which I'm woking on.
Would it also work for the infragistics WebDataGridView? This code comes form a really old post I found, posted in 2003 so based on the developments this is on old way, my code for addin a user is the correct one.
//New Code
Dim User As System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User If Not User.IsInRole("RoleAsString") Then Dim rc As New DotNetNuke.Security.Roles.RoleController() Dim ri As DotNetNuke.Security.Roles.RoleInfo = rc.GetRoleByName(PortalId, "RoleNameAsTring") Dim ui As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo() rc.AddUserRole(PortalId, ui.UserID, ri.RoleID, Null.NullDate) End If
//Old code
Private Sub AddUserToRoles() Dim objRoles As New RoleController
Dim Selected As System.Web.UI.WebControls.CheckBox Dim intRow As Integer Dim DI As DataGridItem Dim TC As TableCell Dim RoleId As Integer
For intRow = 0 To grdServices.Items.Count - 1 DI = grdServices.Items.Item(intRow) TC = DI.Cells(0) Selected = TC.FindControl("chkService") If Selected.Checked = True Then RoleId = CInt(DI.Cells(1).Text) Dim objRole As RoleInfo = objRoles.GetRole(RoleId, PortalSettings.PortalId) If objRole.IsPublic And objRole.ServiceFee = 0.0 Then objRoles.UpdateUserRole(PortalId, User.UserID, RoleId, False) End If Else RoleId = CInt(DI.Cells(1).Text) Dim objRole As RoleInfo = objRoles.GetUserRole(PortalId, User.UserID, RoleId) If Not objRole Is Nothing Then objRoles.DeleteUserRole(PortalId, User.UserID, RoleId) End If End If Next End Sub
it's not the names, since I just paste it as preview, every controlname = cbName.removed the parenthesis and still getting the error
Could it be that it has something to do with the object Username wich could be wrong declared?Since after exploiting the code I found out that after .Value, it asks for a Object As Type.
DirectCast(WebDataGrid1.Rows(i).Items.Item(1).Value, oUsername)
The first thing I notice that's wrong is that the ID of the checkbox is cbAlgemeen so you should use findcontrol("cbAlgemeen").checked and leave off those parenthesis.
Could u try and explain what you did when you found out whats wrong?
Ed, thanks alot for helping me out.
<ig:WebDataGrid ID="WebDataGrid1" runat="server" EnableAjax="False" Height="350px" Width="637px" AutoGenerateColumns="False" style="margin-bottom: 4px"> <Columns> <ig:BoundDataField DataFieldName="UserID" Key="UserID" Hidden="True"> <Header Text="UserID" /> </ig:BoundDataField> <ig:BoundDataField DataFieldName="Username" Key="Username" Width="90px"> <Header Text="Username" /> </ig:BoundDataField> <ig:TemplateDataField Key="Algemeen" Width="90px"> <ItemTemplate> <div class="algemeen" style="text-align: center;"> <asp:CheckBox runat="server" ID="cbAlgemeen" /> </div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Algemeen"></asp:Label> <asp:CheckBox ID="cbAlgemeen" runat="server" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="Financieel" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Financieel" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Financieel"></asp:Label> <asp:CheckBox ID="Financieel" runat="server" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="Licentie" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Licentie" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Licentie"></asp:Label> <asp:CheckBox ID="Licentie" runat="server" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="Applicatie" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="Applicatie" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Applicatie"></asp:Label> <asp:CheckBox ID="Applicatie" runat="server" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> <ig:TemplateDataField Key="UitDienst" Width="90px"> <ItemTemplate> <div style="text-align: center;"> <asp:CheckBox runat="server" ID="UitDienst" /></div> </ItemTemplate> <HeaderTemplate> <div style="text-align: center;"> <asp:Label ID="Label3" runat="server" Text="Uit Dienst"></asp:Label> <asp:CheckBox ID="UitDienst" runat="server" /> </div> </div> </HeaderTemplate> </ig:TemplateDataField> </Columns> </ig:WebDataGrid><asp:Button ID="BtnEditPermissions" runat="server" Text="Edit Permissions" BackColor="Transparent" BorderStyle="None" Font-Bold="True" onClick="BtnEditPermissions_Click" />
Partial Class DesktopModules_Autorisatie_Autorisatie_View Inherits DotNetNuke.Entities.Modules.PortalModuleBase Dim Company As String Dim cSQL As String Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim userInfo As UserInfo userInfo = UserController.GetCurrentUserInfo() If userInfo.UserID = -1 Then End If Company = userInfo.Profile.GetPropertyValue("Company") Dim strSQLconnection As String = ("Data Source=.\SQLExpress;Initial Catalog=dnn2;User ID=vb1;Password=Klompsrv1") cSQL = "SELECT Users.UserID, Users.Username, UserProfile.PropertyValue FROM ProfilePropertyDefinition INNER JOIN UserProfile ON ProfilePropertyDefinition.PropertyDefinitionID = UserProfile.PropertyDefinitionID INNER JOIN Users ON UserProfile.UserID = Users.UserID WHERE (ProfilePropertyDefinition.PropertyName = 'Company') AND (UserProfile.PropertyValue = '" + Company + "')" Dim sqlConnection As New SqlConnection(strSQLconnection) Dim sqlCommand As New SqlCommand(cSQL, sqlConnection) Dim SqlAdapter As New SqlDataAdapter(sqlCommand) Dim DataSet As New DataSet() SqlAdapter.Fill(DataSet) If sqlConnection.State = ConnectionState.Closed Then sqlConnection.Open() End If WebDataGrid1.DataSource = DataSet WebDataGrid1.DataBind() End Sub Protected Sub BtnEditPermissions_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEditPermissions.Click For i As Integer = 0 To WebDataGrid1.Rows.Count - 1 If DirectCast(WebDataGrid1.Rows(i).Items(2).FindControl("Algemeen"), CheckBox).Checked() AndAlso DirectCast(WebDataGrid1.Rows(i).Items.Item(1).Value, Object) Then Dim User As System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User If Not User.IsInRole("Algemeen") Then Dim rc As New DotNetNuke.Security.Roles.RoleController() Dim ri As DotNetNuke.Security.Roles.RoleInfo = rc.GetRoleByName(PortalId, "Algemeen") Dim ui As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo() rc.AddUserRole(PortalId, ui.UserID, ri.RoleID, Null.NullDate) End If End If If DirectCast(WebDataGrid1.Rows(i).Items(3).FindControl("Financieel"), CheckBox).Checked() AndAlso DirectCast(WebDataGrid1.Rows(i).Items.Item(1).Value, Object) Then Dim User As System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User If Not User.IsInRole("Financieel") Then Dim rc As New DotNetNuke.Security.Roles.RoleController() Dim ri As DotNetNuke.Security.Roles.RoleInfo = rc.GetRoleByName(PortalId, "Financieel") Dim ui As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo() rc.AddUserRole(PortalId, ui.UserID, ri.RoleID, Null.NullDate) End If End If If DirectCast(WebDataGrid1.Rows(i).Items(4).FindControl("Licentie"), CheckBox).Checked() AndAlso DirectCast(WebDataGrid1.Rows(i).Items.Item(1).Value, Object) Then Dim User As System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User If Not User.IsInRole("Licentie") Then Dim rc As New DotNetNuke.Security.Roles.RoleController() Dim ri As DotNetNuke.Security.Roles.RoleInfo = rc.GetRoleByName(PortalId, "Licentie") Dim ui As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo() rc.AddUserRole(PortalId, ui.UserID, ri.RoleID, Null.NullDate) End If End If If DirectCast(WebDataGrid1.Rows(i).Items(5).FindControl("Applicatie"), CheckBox).Checked() AndAlso DirectCast(WebDataGrid1.Rows(i).Items.Item(1).Value, Object) Then Dim User As System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User If Not User.IsInRole("Applicatie") Then Dim rc As New DotNetNuke.Security.Roles.RoleController() Dim ri As DotNetNuke.Security.Roles.RoleInfo = rc.GetRoleByName(PortalId, "Applicatie") Dim ui As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo() rc.AddUserRole(PortalId, ui.UserID, ri.RoleID, Null.NullDate) End If End If If DirectCast(WebDataGrid1.Rows(i).Items(6).FindControl("UitDienst"), CheckBox).Checked() AndAlso DirectCast(WebDataGrid1.Rows(i).Items.Item(1).Value, Object) Then Dim User As System.Security.Principal.IPrincipal User = System.Web.HttpContext.Current.User If Not User.IsInRole("UitDienst") Then Dim rc As New DotNetNuke.Security.Roles.RoleController() Dim ri As DotNetNuke.Security.Roles.RoleInfo = rc.GetRoleByName(PortalId, "UitDienst") Dim ui As DotNetNuke.Entities.Users.UserInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo() rc.AddUserRole(PortalId, ui.UserID, ri.RoleID, Null.NullDate) End If End If Next End SubEnd Class
Put up the markup and code and I'll try to make it work.