C# 如何在网格视图中获取隐藏字段的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9915086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i get the value of hidden field in grid view?
提问by leventkalayz
the order number of hidden field in grid view is 7.
网格视图中隐藏字段的顺序号为7。
when i click the button the line
当我点击按钮时
string sValue = ((HiddenField)GridView1.SelectedRow.Cells[7].FindControl("HiddenField1")).Value;
gives error which is "Object reference not set to an instance of an object."
给出错误“对象引用未设置到对象的实例”。
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%#Eval("RSS_ID")%>'/>
</ItemTemplate>
</asp:TemplateField>
c# side
c#侧
else if (e.CommandName == "View")
{
string sValue = ((HiddenField)GridView1.SelectedRow.Cells[7].FindControl("HiddenField1")).Value;
}
采纳答案by NiK
did you try this?
你试过这个吗?
HiddenField field = (HiddenField)GridView.Rows[GridView.SelectedIndex].FindControl("HiddenField1");
If yes, how about this one?
如果是,这个怎么样?
HiddenField field = GridView1.Rows[e.RowIndex].FindControl("HiddenField1") as HiddenField;
Here is another one you could try,
这是你可以尝试的另一种,
if(e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField field = e.Row.FindControl("HiddenField1") as HiddenField;
}
Hope this helps...cheers
希望这有助于...干杯
回答by Dave D
Drop the Cells part
删除单元格部分
If you have the selected row:
如果您有选定的行:
string sValue = ((HiddenField)GridView1.SelectedRow.FindControl("HiddenField1")).Value;
If you have e.rowIndex from the command argument:
如果您有来自命令参数的 e.rowIndex:
string sValue = ((HiddenField)GridView1.Rows[e.rowIndex].FindControl("HiddenField1")).Value;
回答by walther
You're trying to access SelectedRow, even though I don't see the code, when you actually select the row. My guess is that you're using only some custom command button, that doesn't really set the selected row. Fix that and it should work.
当您实际选择该行时,您正试图访问 SelectedRow,即使我没有看到代码。我的猜测是您只使用了一些自定义命令按钮,并没有真正设置所选行。解决这个问题,它应该可以工作。
If you can't/don't want to, you would need to write yourself some method to find the row you want and then applying FindControl method to access your hidden field, getting the value...
如果你不能/不想,你需要自己写一些方法来找到你想要的行,然后应用 FindControl 方法来访问你的隐藏字段,获取值......
Or try to post a more complete source code....
或者尝试发布更完整的源代码....

