<!-- Article image -->
<!-- Add the rest of your HTML here -->
A recent project I was working on required a user to select a number of options from a list. Instead of using a mulit-select list box which didn't really fit into the design of the site we decided to make a reusable control that would add a checkbox to a
Using the code
To use the checkbox column in a
DataGrid it's simply a matter of registering the tag at the top of the page:
<%@ Register TagPrefix="chkbox" Namespace="DataGridControls"
Then to add the checkbox column to the
<asp:DataGrid ID="dgTestGrid" Runat="server" AutoGenerateColumns=True
CheckBoxColumn class is pretty straight forward:
The class exposes 2 properties:
SelectedDataKeys: Returns an
ArrayList with the
SelectedIndexes: Returns an
Int32 with the
To find out which checkbox has been selected:
protected void btnSubmit_Click(object sender, EventArgs e)
CheckBoxColumn chkbox = (CheckBoxColumn) dgTestGrid.Columns;
foreach(object datakeyfield in chkbox.SelectedDataKeys)
Response.Write(datakeyfield.ToString() + "<br>");
That's pretty much it, the
DataKeyField of the
DataGrid can be of any type. The sample I've included binds a
DataTable to the
DataGrid, you can change the
DataKeyField from "
int) to "
string) to see the code working with different types.