Click here to Skip to main content
16,011,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this code where on the click of a button i want to get the Empid of all those checkboxes that are checked in each row. but the string variable str is always empty and doesnt take any value.why is this so? kindly help.

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)
            {
                if (str == "")
                {
                    str = GridView1.Rows[i].Cells[0].Text.ToString();
                }
                else
                {
                    srr = str + "," + GridView1.Rows[i].Cells[1].Text.ToString();
                }
            }
        }
        Session["card_id"] = str;
        Response.Redirect("ID.aspx");
    }
}
.
Posted
Comments
_Amy 20-Mar-13 5:23am    
Are you using TemplateField or BondField?

1 solution

Hi Avinash,
I think Cells[0] contains your checkbox and Cells[1] contains your EmpID
The below code will solve your problem.
C#
protected void btn_3id_Click(object sender, EventArgs e)
{
    ArrayList str = new ArrayList();

    foreach (GridViewRow rows in GridView1.Rows)
    {
        CheckBox chk = (CheckBox)GridView1.Rows[rows.RowIndex].FindControl("CheckBox1");
        if (chk.Checked)
        {
            str.Add(GridView1.Rows[rows.RowIndex].Cells[1].Text.ToString());
        }
    }
    Session["card_id"] = str;
    Response.Redirect("ID.aspx");
}

Please let me know if you still facing any issues.
Regards,
Manoj
 
Share this answer
 
v2

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