Click here to Skip to main content
15,898,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

i am working as a developer. what my problem is i create one grid with check box control in item templete. just i want to capture is check box is checked or not.
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
   if (((CheckBox)          (Gridview1.Rows[i].FindControl("CkbSelection"))).Checked==true )
{
//code here 
}
 
}

but i got the check box values as false if i checked also.what to do.plz any one help me.

thank u.
Posted
Updated 8-Nov-11 0:52am
v2
Comments
hitech_s 8-Nov-11 6:53am    
placed pre tags

it works fine for me check once still if you are not getting reply me

GridViewRowCollection rc = gvArea.Rows;
                foreach (GridViewRow gRow in rc)
                {
                    CheckBox cBox = (CheckBox)gRow.FindControl("chkSelect");

                    if (cBox.Checked == true)
                    {
                       

                    }
                }
 
Share this answer
 
Actually, if you have that problem means where you have placed that CheckBox..?

ASP.NET
<asp:templatefield headertext="checkbox" xmlns:asp="#unknown">
    <itemtemplate>
    
         <asp:checkbox id="cb" runat="server" />
    
    </itemtemplate>
    <edititemtemplate>
   
         <asp:checkbox id="cb1" runat="server" />
                     
    </edititemtemplate>
    </asp:templatefield>


If you placed that one in ItemTemplate (ie, for 'cb' checkbox)then you can directly access it through
the code which is written by yourself and hitech_ssc

C#
protected void b1_Click(object sender, EventArgs e)
       {
           foreach (GridViewRow item in gv.Rows)
           {
               CheckBox checkBox = (CheckBox)item.FindControl("cb");
               if (checkBox.Checked)
               {
               }

           }
       }


and if you want to access the checkbox=cb1, then
onEditing event

C#
void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {

           CheckBox checkBox = (CheckBox)gv.Rows[e.RowIndex].FindControl("cb1");
           bool isChecked = checkBox.Checked;
           if (isChecked)
           {
           }
       }
 
Share this answer
 
Hi,

Try this-

C#
for (GridViewRow row in Gridview1.Rows)
{
   if(row.RowType == DataControlRowType.DataRow)
   {
      if ((CheckBox)row.FindControl("CkbSelection")).Checked)
      {
        //code here
      }
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900