Using Checkboxes inside GridView Control
Add a template field and add a checkbox inside the item template.
Html code:
———-
<table>
<tr>
<td>
<asp:GridView ID=”GridView1″ Width=”500px” runat=”server”>
<Columns>
<asp:BoundField DataField=”ClientID” HeaderText=”Client ID”></asp:BoundField>
<asp:BoundField DataField=”Name_of_Client” HeaderText=”Client”></asp:BoundField>
<asp:TemplateField HeaderText=”Select to Print”>
<ItemTemplate>
<asp:CheckBox ID=”chkSelect” runat=”server” />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
<input id=”Submit1″ type=”submit” value=”Print Specific” runat =server onserverclick=”Submit1_ServerClick” />
</td>
</tr>
</table>
Code Behind:
————
The following code get the value of the checkbox whether it is checked or not and displays the corresponding name of the clients.
protected void Submit1_ServerClick(object sender, EventArgs e)
{
// StringBuilder object
StringBuilder str = new StringBuilder();
// Select the checkboxes from the GridView control
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl(”chkSelect”)).Checked;
if (isChecked)
{
// Column 1 is the name column
str.Append(GridView1.Rows[i].Cells[1].Text + “#”);
}
}
if (str.ToString() != “”)
{
Response.Write(str.ToString());
}
}
