ASP.NET C# - Trouble with checkbox
Anyone know why the code below does not work. All I want is to check a checkbox, then hit a button, then for the value of the DataKeyField in the DataGrid to show up as the Text property in a Label called textboxMessage. The DataGrid is working OK as if the foreach loop, the bit where its coming unstuck is the check box definition or test to see if it is checked. I don't get a compilation error - just that it doesn't work.
<asp:DataGrid id="DataGrid1" DataKeyField="RequestID" runat="server" Font-Size="X-Small">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="checkboxSelect" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
private void Button1_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem DGridItem in DataGrid1.Items)
{
CheckBox myCheckbox = (CheckBox)DGridItem.FindControl("checkboxSelect");
if(myCheckbox.Checked == true)
{
textboxMessage.Text= DataGrid1.DataKeys[DGridItem.ItemIndex].ToString();
}
}
}
|