Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi there,

I have an asp application which I have built in security for the users. I have a page where I list all users and their roles. When I click a user it shows me all the roles they are assigned to and some which are not but available to assign. I want it when the user checks a new role and click ok to save the role under their name. I have created a stored procedure in sql to do that but I just cant get the vb correct behind the button. Here is what I have so far:

So basically i want to be able to select a role by checking box and click ok to store it then if i uncheck it, it removes the user from that role.

Code behind the button

Protected Sub btnUpdateRole_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdateRole.Click
        lblError.Text = ""
        Dim usergroup(0) As String
        usergroup(0) = ccode.wfgen(grdRoles.SelectedRow.Cells(1).Text)
        For Each row As GridViewRow In grdRoles.Rows
            Dim db As CheckBox = row.FindControl("CheckBox1")
            If db IsNot Nothing AndAlso db.Checked Then
                Try
                    'Roles.AddUsersToRoles(usergroup, row.Cells(1).Text)
                Catch ex As Exception
                End Try
            Else
                Try
                    'Roles.RemoveUserFromRoles(usergroup(0), row.Cells(1).Text)
                Catch ex As Exception
                End Try
            End If
        Next
        pnlAddCancel.Visible = False
        pnlRoles.Visible = False
        btnAddUser.Visible = True
        btnManageRoles.Visible = True
        GridView1.Columns(6).Visible = True
        GridView1.Columns(5).Visible = True
        GridView1.Columns(4).Visible = True
        GridView1.SelectedIndex = -1
    End Sub


Code behind the asp.net page (gridview)

XML
<asp:GridView ID="grdRoles" runat="server" DataSourceID="SqlDataSource2" AutoGenerateColumns="False"
            Width="100%" AllowSorting="True" DataKeyNames="Role_Name" Font-Names="Cambria"
            Font-Size="Small">
            <Columns>
                <asp:BoundField DataField="Role_Name" HeaderText="Role_Name" SortExpression="Role_Name"
                    ItemStyle-BackColor="White">
                    <ItemStyle BackColor="White" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="CheckBox" ShowHeader="False" SortExpression="CheckBox"
                    ItemStyle-BackColor="White">
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Chk") %>' />
                    </ItemTemplate>
                    <ItemStyle BackColor="White" />
                </asp:TemplateField>
            </Columns>
            <PagerSettings Mode="NumericFirstLast" />
            <SelectedRowStyle BackColor="White" />
            <HeaderStyle BackColor="#A9C8EF" ForeColor="White" />
        </asp:GridView>


Thanks in advance
Posted
Updated 12-May-11 1:57am
v4

1 solution

hi there,
u need to do this when u assign the checkbox value to ur new checkbox item
eg:
dim chkItem as CheckBox
chkItem = DirectCast(<grid>.Rows(<index>).FindControl("chkCheck"), CheckBox)
<grid> : name of ur grid
<index>: row index
chkCheck: column name where u have the checkbox column

hope it helps. let me know :)
 
Share this answer
 
Comments
Geofferz 12-May-11 6:00am    
I have just updated the question and added more code! When i tried with your code it did not work! Maybe it will help understand it better with the new code added. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900