访问gridview中的复选框 [英] Accessing checkbox in a gridview
问题描述
我有一个gridview,其中我在每行使用复选框。我试图访问每一行的复选框,并试图找出哪些复选框已被检查。当我尝试运行下面的代码时,条件总是为假,并且代码永远不会达到内部if条件。请提前帮助我。
<前lang =cs> 受保护 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() ;
}
}
}
会话[ Card_id] = str;
Response.Redirect( ID.aspx);
}
循环通过GridView Rows。试试这个:
protected void btn_3id_Click ( object sender,EventArgs e)
{
string str = ;
string srr = ;
foreach (GridViewRow行 GridView1.Rows)
{
if (((CheckBox)row.FindControl( CheckBox1 ))。已检查)
{
if (str == )
{
str = row.Cells [ 0 ] .Text.ToString();
}
else
{
srr = str + , + row.Cells [ 0 ]。Text.ToString();
}
}
会话[ Card_id] = str ;
Response.Redirect( ID.aspx);
}
}
--Amit
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.
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");
}
Loop through GridView Rows. Try this:
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
这篇关于访问gridview中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!