Add checkbox with CheckedChanged Event to a Dynamic GridView

前端 未结 2 1481
遥遥无期
遥遥无期 2020-12-18 14:31

I want to dynamically add a checkbox to a dynamic GridView along with an Event.

i.e. For the grid I have to add check boxes dynamically checked or unchecked accord

相关标签:
2条回答
  • 2020-12-18 15:26

    I'm sorry, follow this

    Place a check box in gridview

    this is an example HTML Code to declare a checkbox in gridview

                   <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.
    }
    
    0 讨论(0)
  • 2020-12-18 15:35

    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.
        }
    
    0 讨论(0)
提交回复
热议问题