ASP.Net Gridview复选框 [英] ASP.Net Gridview checkbox
问题描述
你好,
我有一个为用户内置安全性的asp应用程序.我有一个页面,其中列出了所有用户及其角色.当我单击一个用户时,它会向我显示他们分配给的所有角色,以及一些无法分配的角色.当用户检查新角色并单击确定"以其名称保存该角色时,我需要它.我已经在sql中创建了一个存储过程来做到这一点,但是我无法正确获取按钮后面的vb.这是我到目前为止的内容:
因此,基本上,我希望能够通过选中复选框并单击确定"来存储角色,然后如果我取消选中它,则会将用户从该角色中删除.
按钮后面的代码
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
asp.net页面(gridview)背后的代码
Code behind the asp.net page (gridview)
<pre lang="xml"><asp:GridView ID="grdRoles" runat="server" DataSourceID="SqlDataSource2" AutoGenerateColumns="False"<br />
Width="100%" AllowSorting="True" DataKeyNames="Role_Name" Font-Names="Cambria"<br />
Font-Size="Small"><br />
<Columns><br />
<asp:BoundField DataField="Role_Name" HeaderText="Role_Name" SortExpression="Role_Name"<br />
ItemStyle-BackColor="White"><br />
<ItemStyle BackColor="White" /><br />
</asp:BoundField><br />
<asp:TemplateField HeaderText="CheckBox" ShowHeader="False" SortExpression="CheckBox"<br />
ItemStyle-BackColor="White"><br />
<ItemTemplate><br />
<asp:CheckBox ID="CheckBox1" runat="server" Checked=''<%# Bind("Chk") %>'' /><br />
</ItemTemplate><br />
<ItemStyle BackColor="White" /><br />
</asp:TemplateField><br />
</Columns><br />
<PagerSettings Mode="NumericFirstLast" /><br />
<SelectedRowStyle BackColor="White" /><br />
<HeaderStyle BackColor="#A9C8EF" ForeColor="White" /><br />
</asp:GridView></pre><br />
在此先感谢
Thanks in advance
推荐答案
您好,
当您将复选框值分配给新的复选框项目时,您需要执行此操作
例如:
将chkItem暗淡为CheckBox
chkItem = DirectCast(< grid> .Rows(< index>).FindControl("chkCheck"),CheckBox)
< grid> :您的网格名称
< index> ;:行索引
chkCheck:列名,其中您具有复选框列
希望能帮助到你.让我知道:)
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 :)
这篇关于ASP.Net Gridview复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!