将CheckedChanged事件的复选框添加到动态GridView

Ani*_*ran 2 c# asp.net

我想动态地向动态GridView添加一个复选框以及一个Event.

即对于网格,我必须根据数据库添加动态选中或取消选中的复选框.通过单击复选框本身,我想更新数据库.

为此,我需要动态加载事件以及复选框.

我完成的是一个静态版本,在这里展出:

在数据库RoleID(管理员,采购官等)中,存储ActivityID(离开应用程序等)和OperationID(保存,编辑等).

对于Admin(roleid 1),第一行表示允许活动离开应用程序(Activityid 3)的保存操作(OperationID 1).

Kri*_*ota 6

对不起,请关注此事

在gridview中放置一个复选框

这是一个示例HTML代码,用于在gridview中声明一个复选框

               <asp:TemplateField HeaderText="chkbox">
                   <ItemTemplate>
                       <asp:CheckBox ID="CheckBox1" runat="server"  AutoPostBack="true"
                           oncheckedchanged="CheckBox1_CheckedChanged"  />
                   </ItemTemplate>
               </asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)

现在关于复选框的事件

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.
}
Run Code Online (Sandbox Code Playgroud)