我有这个代码,点击一个按钮,我想获得每行中检查的所有复选框的Empid。但是字符串变量str总是空的并且没有任何值。为什么会这样呢?请帮助
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");
}
}
答案 0 :(得分:2)
如果您的控件名称是Label,则使用
str = ((Label)GridView1.Rows[i].FindControl("yourlabelid")).Text;
取代
str = GridView1.Rows[i].Cells[0].Text.ToString();