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
Hi Ed,
I know that, its just a random number I added as example, I got 6 colums, 0 = userid, 1st = Username 2nd, 3rd, 4th , 5th, 6th are checkbox controls
that not the problem, the problem is that I get the error 'Object reference not set to an instance of an object'
Wich I dont know why it shows, since the code should work
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.
Ed
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.
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
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