C# 将带有 CheckedChanged 事件的复选框添加到动态 GridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9527908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add checkbox with CheckedChanged Event to a Dynamic GridView
提问by Anish Karunakaran
I want to dynamically add a checkbox to a dynamic GridView along with an Event.
我想将复选框与事件一起动态添加到动态 GridView。
i.e. For the grid I have to add check boxes dynamically checked or unchecked according to the Database. And by clicking the checkbox itself I want to update the database.
即对于网格,我必须根据数据库添加动态选中或取消选中的复选框。通过单击复选框本身,我想更新数据库。
For this I need the Event to also be dynamically loaded along with the checkbox.
为此,我还需要将事件与复选框一起动态加载。
What I have completed is a static version and is exhibited here:
我完成的是一个静态版本,在这里展示:
In database RoleID(Admin,Purchase Officer etc), ActivityID(Leave application etc) and OperationID(Save,Edit Etc) are stored.
在数据库 RoleID(Admin,Purchase Officer etc) 中,存储了 ActivityID(Leave application etc) 和 OperationID(Save,Edit Etc)。
First row implies for Admin(roleid 1) Save operation(OperationID 1) is allowed for activity Leave application(Activityid 3).
第一行暗示 Admin(roleid 1) 允许活动 Leave application(Activityid 3) 的 Save 操作(OperationID 1)。
采纳答案by sinanakyazici
if you are adding checkboxes at runtime, when you add checkbox, the checkbox event needs to be defined.
如果在运行时添加复选框,添加复选框时,需要定义复选框事件。
For example :
例如 :
TableCell tcCheckCell = new TableCell();
var checkBox = new CheckBox();
checkBox.CheckedChanged += checkBox_CheckedChanged;
tcCheckCell.Controls.Add(checkBox);
gridView.Rows[0].Cells.AddAt(0, tcCheckCell);
void checkBox_CheckedChanged(object sender, EventArgs e)
{
//do something: You can use Krishna Thota's Code.
}
回答by Krishna Thota
I'm sorry, follow this
对不起,按照这个
Place a check box in gridview
在 gridview 中放置一个复选框
this is an example HTML Code to declare a checkbox in gridview
这是在 gridview 中声明复选框的示例 HTML 代码
<asp:TemplateField HeaderText="chkbox">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true"
oncheckedchanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
Now about the event for the checkbox
现在关于复选框的事件
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
int index = row.RowIndex;
CheckBox cb1 = (CheckBox)Gridview.Rows[index].FindControl("CheckBox1");
string checkboxstatus;
if (cb1.Checked == true)
checkboxstatus = "YES";
else if(cb1.Checked == false)
checkboxstatus = "NO";
//Here Write the code to connect to your database and update the status by
//sending the checkboxstatus as variable and update in the database.
}

