Checkbox inside gridview.
-
Hi, I have a checkbox inside a gridview which is bound to data at runtime except the checkbox templatefield. At server side I am trying to capture the row details for the row which is selected. I get the checkbox but the checkbox.checked is always false even when it is selected.
protected void btnSelect_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{CheckBox chk = (CheckBox)row.FindControl("chkAgency"); if (chk != null && chk.Checked) { Response.Cookies\["thistestcookie"\].Value = row.Cells\[0\].Text.ToString(); } }
how do i get the checked checkbox row details ?! Thanks, Fifi
-
Hi, I have a checkbox inside a gridview which is bound to data at runtime except the checkbox templatefield. At server side I am trying to capture the row details for the row which is selected. I get the checkbox but the checkbox.checked is always false even when it is selected.
protected void btnSelect_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{CheckBox chk = (CheckBox)row.FindControl("chkAgency"); if (chk != null && chk.Checked) { Response.Cookies\["thistestcookie"\].Value = row.Cells\[0\].Text.ToString(); } }
how do i get the checked checkbox row details ?! Thanks, Fifi
lets see the markup where you defined the checkbox ?
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/
-
lets see the markup where you defined the checkbox ?
Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/
<Columns> <asp:BoundField DataField = "Name1" HeaderText = "NAME" /> <asp:BoundField DataField ="Name2" HeaderText = "NAME2"/> <asp:BoundField DataField = "Name3" HeaderText = "NAME3"/> <asp:TemplateField> <ItemTemplate> <asp:CheckBox id ="chkAgency" runat="server" Enabled /> </ItemTemplate> </asp:TemplateField> </Columns>
-
<Columns> <asp:BoundField DataField = "Name1" HeaderText = "NAME" /> <asp:BoundField DataField ="Name2" HeaderText = "NAME2"/> <asp:BoundField DataField = "Name3" HeaderText = "NAME3"/> <asp:TemplateField> <ItemTemplate> <asp:CheckBox id ="chkAgency" runat="server" Enabled /> </ItemTemplate> </asp:TemplateField> </Columns>
ok i see where your problem is. You assume if you add a checkbox, it will carry the ID of the checked field , that is not true. I normally create an extra template field and bind the label with the value and as you did on the server side, i find the label and get the value. I have compiled an example and this will help you html
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" DataSourceID="SqlDataSource1"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chkAgency" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblid" runat="server" Text='<%# Eval("ID")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField = "ID" HeaderText = "ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField ="Descr" HeaderText = "Descr" SortExpression="Descr"/> <asp:BoundField DataField = "LongName" HeaderText = "LongName" SortExpression="LongName"/> <asp:BoundField DataField="Capacity" HeaderText="Capacity" SortExpression="Capacity" /> <asp:BoundField DataField="Note" HeaderText="Note" SortExpression="Note" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TTBLv3MasterConnectionString %>" SelectCommand="select \* from dbo.TBL\_VENUE"></asp:SqlDataSource>
<asp:Button ID="btntest" Text="Test" runat="server" onclick="btntest_Click" />
and the server side
protected void btntest_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{CheckBox chk = (CheckBox)row.FindControl("chkAgency"); Label lblid = (Label)row.FindControl("lblid"); List<string> lstList = new List<string>(); if (chk != null && chk.Checked) { //Response.Cookies\["thistestcookie"\].Value = row.Cells\[0\].Text.ToString(); lstList.Add(lblid.Text); }