void DataGrid1_OnItemdataBound(object Sender, DataGridItemEventArgs e)
{
try
{
CheckBoxList Chk = (CheckBoxList)e.Item.FindControl("checkboxlist1");
Chk.Items[0].Selected = true; //you can set to the dataset value here
Chk.Items[2].Selected = true;
//string[] myArray = new string[3] { "Good", "Bad", "So-So" };
//Chk.DataSource = myArray;
//Chk.DataBind();
//Chk.Items[0].Selected = true;
}
catch(Exception exp)
{
lbl.Text += exp.Message + " : ";
}
}
void Check_Clicked(Object sender, EventArgs e)
{
//How can I get here the checkboxes that were checked?
CheckBoxList chk = (CheckBoxList)sender;
if (chk.SelectedIndex == -1)
{
lbl.Text = "0 Items" + " clicked";
}else {
lbl.Text = "Item " + (chk.SelectedIndex + 1) + " clicked";
}
}
Html code:
<asp:TemplateColumn HeaderText="Selected">
<ItemTemplate>
asp:CheckBoxList id="checkboxlist1"
AutoPostBack="True"
CellPadding="5"
CellSpacing="5"
RepeatLayout="Flow"
TextAlign="Right"
OnSelectedIndexChanged="Check_Clicked"
runat="server">
<asp:ListItem>Good</asp:ListItem>
<asp:ListItem>Bad</asp:ListItem>
<asp:ListItem>So-So</asp:ListItem>
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateColumn>