如何通过gridview外的复选框获取id选择行 [英] How to get the id selected row through checkbox outside gridview
本文介绍了如何通过gridview外的复选框获取id选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我在gridview的每一行都有一个gridview和复选框。我想获取所选复选框row.so的ID,我正在尝试以下代码
Hello,
I have an gridview and checkbox on every row of an gridview.I want to get the id of selected checkbox row.so, i am trying following code
foreach (GridViewRow gvr in grdReports.Rows)
{
CheckBox chkSelectEmp = (CheckBox)grdReports.FindControl("chkSelect");
Label lblEmpId = (Label)grdReports.FindControl("lblEmpRefId");
if (chkSelectEmp.Checked == true)
Session["EmpRefId"] = lblEmpId.Text.ToString();
}
但是收到以下错误
对象引用未设置为对象的实例。
on
if(chkSelectEmp.Checked == true)
but getting following error
Object reference not set to an instance of an object.
on
if (chkSelectEmp.Checked == true)
推荐答案
试试这个
try this
foreach (GridViewRow row in grdReports.Rows)
{
CheckBox chkSelectEmp = row.FindControl("chkSelect") as CheckBox;
if (chkSelectEmp .Checked == true)
{
Label lblEmpId = (Label )grdReports.Rows[rowIndex].Cells[6].FindControl("lblEmpRefId");
Session["EmpRefId"] = lblEmpId.Text.ToString();
}
}
这里,你提到单元格[6]来纠正LblEmpRefId的索引
here , you metion Cells[6] to Correct Index of "LblEmpRefId"
当使用findcontrol作为标签和复选框时,将代码grdReports替换为行。
试试这个
replace code grdReports to row when use findcontrol for label and checkbox .
try this
foreach (GridViewRow row in grdReports.Rows)
{
CheckBox chkSelectEmp = row.FindControl("chkSelect") as CheckBox;
if (CheckBox1.Checked == true)
{
Label lblEmpId = row.FindControl("lblEmpRefId") as Label;
Session["EmpRefId"] = lblEmpId.Text.ToString();
}
}
更改
change
grdReports.FindControl
to
to
gvr.FindControl
这篇关于如何通过gridview外的复选框获取id选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文