Performing task on checkbox checked event in GridView

Step 1

(Add the following code to your  Default.aspx page)

Take a gridview and take a checkbox as a templatefield and take a button:

<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                    AutoGenerateColumns="False" CellPadding="4">
                    <RowStyle BackColor="Beige" ForeColor="#333333" HorizontalAlign="Center" Font-Bold="true"/>

                    <Columns>
                  <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server"/>
                    </ItemTemplate>
                    </asp:TemplateField>
                        <asp:BoundField DataField="TYPE" HeaderText="Type" />
                        <asp:BoundField DataField="CHANNEL" HeaderText="Chanel" />
                        <asp:BoundField DataField="CALLERID" HeaderText="Caller ID" />

                        <asp:BoundField DataField="RINGING_TIME" HeaderText="RingDuration" />
                        <asp:BoundField DataField="DURATION" HeaderText="CallDuration" />
                    </Columns>
                </asp:GridView></div>

// taking a button
<div>
<asp:Button ID="btn1" runat="server" Text="Play" onclick="btn1_Click"
           />
 
</div>

Step 2

Now finding which checkbox is checked by the following code on the click event of the button in Default.cs page.

 protected void btn1_Click(object sender, EventArgs e)
    { 
foreach (GridViewRow item in GridView1.Rows)
        {

            if (item.RowType == DataControlRowType.DataRow)
            {
           CheckBox chk = (CheckBox)(item.Cells[0].FindControl("CheckBox1"));

                if (chk.Checked)
                {
                    //perform your task here
                }
            }
        }
}

Ebook Download
View all
Learn
View all