J
Joe Bloggs
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. 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
ataGrid 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
ataGrid>
private void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach(DataGridItem DGridItem in DataGrid1.Items)
{
CheckBox myCheckbox =
(CheckBox)DGridItem.FindControl("checkboxSelect");
if(myCheckbox.Checked == true)
{
sb.Append(DataGrid1.DataKeys[DGridItem.ItemIndex].ToString());
}
}
Label.Text = sb.ToString();
}
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. 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

Font-Size="X-Small">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="checkboxSelect"
Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp

private void Button1_Click(object sender, System.EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach(DataGridItem DGridItem in DataGrid1.Items)
{
CheckBox myCheckbox =
(CheckBox)DGridItem.FindControl("checkboxSelect");
if(myCheckbox.Checked == true)
{
sb.Append(DataGrid1.DataKeys[DGridItem.ItemIndex].ToString());
}
}
Label.Text = sb.ToString();
}