在gridview中检查的每个复选框的字符串值为空 [英] string value empty for each checkbox checked in a gridview

查看:73
本文介绍了在gridview中检查的每个复选框的字符串值为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,点击一个按钮,我想获得每行中检查的所有复选框的Empid。但是字符串变量str总是空的并且没有任何值。为什么会这样呢?请帮助。



 受保护  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() ;
}
}
}
会话[ card_id] = str;
Response.Redirect( ID.aspx);
}
}

解决方案

您好Avinash,

我认为Cells [ 0]包含您的复选框,单元格[1]包含您的EmpID

以下代码将解决您的问题。

  protected   void  btn_3id_Click( object  sender,EventArgs e)
{
ArrayList str = new ArrayList();

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



如果您仍然遇到任何问题,请告诉我。

问候,

Manoj


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.

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");
    }
}

.

解决方案

Hi Avinash,
I think Cells[0] contains your checkbox and Cells[1] contains your EmpID
The below code will solve your problem.

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


这篇关于在gridview中检查的每个复选框的字符串值为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆