January 31, 2007

Reading value of a CheckBox in DataGrid

Want to know how to get the value of a check box in a Template Field inside a DataGrid? Check it out:

        Dim chkMiss As CheckBox

' Go through each row and find a check box
For Each i As DataGridItem In dgActive.Items
chkMiss = CType(i.FindControl("chkMiss"), CheckBox)

' Check if miss was selected
If chkMiss.Checked Then
' Use my checkbox
End If
Next

That's all you need on the server-side code, and now for the HTML... Inside your datagrid just create a template column and it should look something like:

  <asp:TemplateColumn HeaderText="Select Check Box">
<ItemTemplate>
<asp:CheckBox id="chkMiss" Text='SomeText' runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

And you're done! The same method applies to all other controls as well.

kick it on DotNetKicks.com

kick it on DotNetKicks.com

1 comment:

Unknown said...

Very usefull article.