Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i have a gridview in which i am using checkbox in each row. i am trying to access checkbox of each row and trying to find out which checkboxes have been checked.buut when i try to run the below code.the condition always stands to be false and the inner if condition is never reached by the code.kindly help me.thanks in advance.

C#
protected void btn_3id_Click(object sender, EventArgs e)
        {
            string str = "";
            string srr = "";
            for (int i = 0; i < GridView1.Rows.Count;i++ )
            {
                CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                if (chk.Checked==true)
                {
                    if (str == "")
                    {
                        str = GridView1.Rows[i].Cells[0].Text.ToString();
                    }
                    else
                    {
                        srr = str + "," + GridView1.Rows[i].Cells[0].Text.ToString();
                    }
                }
            }
            Session["Card_id"] = str;
            Response.Redirect("ID.aspx");
        }
Posted

1 solution

Loop through GridView Rows. Try this:
C#
protected void btn_3id_Click(object sender, EventArgs e)
{
    string str = "";
    string srr = "";
    foreach (GridViewRow row in GridView1.Rows)
    {
       if (((CheckBox)row.FindControl("CheckBox1")).Checked)
       {
            if (str == "")
            {
                str = row.Cells[0].Text.ToString();
            }
            else
            {
                srr = str + "," + row.Cells[0].Text.ToString();
            }
       }
       Session["Card_id"] = str;
       Response.Redirect("ID.aspx");
    }
}



--Amit
 
Share this answer
 
v2
Comments
Abhinav S 20-Mar-13 0:25am    
Correct. A 5.
_Amy 20-Mar-13 0:26am    
Thanks Abhinav. :)
avinash_thakur86 20-Mar-13 1:29am    
thank you amit for the solution...i was stupid as i had kept gridview code out of (!Ispostback ) so gridview was being loaded every time the page was posted back...and checkboxes were getting unchecked...now my code is working fine...thanks man!
_Amy 20-Mar-13 1:30am    
Welcome. :)

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