如何通过gridview外的复选框获取id选择行

编程入门 行业动态 更新时间:2024-10-10 11:24:26
本文介绍了如何通过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选择行

本文发布于:2023-11-22 00:28:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615270.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   gridview   id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!