Hi,
You have to maintain hidden field List and property (get,set) for this pupose. Bind checked item id's in to string list or int list.
add or remove id from list. this happen checkbox's checkchange event and you can catch the checked or unchecked item id in side the event and add or remove from the list and set to the property which maintain the values in side the hidden field. If you want more coding level help, please let me know.
List<string> list = new List<string>();
list.Add("1");
let me update solution with basic codes that you need to improve.
my aspx page
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
>
<Columns>
<asp:TemplateField HeaderText="Name"
SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("FirstName") %>'></asp:Label>
<asp:CheckBox ID="CheckBoxDone" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxDone_CheckedChanged" />
<asp:Label ID="Label2" runat="server"
Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="hf1" runat="server"></asp:HiddenField>
</div>
</form>
and i have bind the data table in to the grid view inside the page load event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = FillDataTable();
GridView1.DataBind();
List<string> strList = new List<string>();
}
}
this is fill data method
private static DataTable FillDataTable()
{
DataTable dt = new DataTable("dttbl");
DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("ID", typeof(int)));
dt.Columns.Add(new System.Data.DataColumn("FirstName", typeof(string)));
dr = dt.NewRow();
dr["ID"] = 1;
dr["FirstName"] = "Dinidu";
dt.Rows.Add(dr);
return dt;
}
this is property
public List<string> MyProperty {
get{
if (ViewState["val"] == null)
{
ViewState["val"] = new List<string>();
} return (List<string>)ViewState["val"];
}
set
{ ViewState.Add("val", value); }
}
this is checked change event
protected void CheckBoxDone_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
string x = ((Label)((CheckBox)sender).FindControl("Label2")).Text;
this.MyProperty.Add(x)
}
else
{
}
}