Linux 访问 gridview hiddenfield

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3676987/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:06:37  来源:igfitidea点击:

accessing gridview hiddenfield

c#asp.netgridview

提问by phil crowe

' />

' />

I want to access the value in the hidden field in my code behind. I know i need to do this when the item is bound but i cant seem to work out how to do it.

我想访问我后面代码中隐藏字段中的值。我知道我需要在项目绑定时执行此操作,但我似乎无法弄清楚如何执行此操作。

protected void addLabelsWhereNeeded(object sender, EventArgs e)
{

   // Get Value from hiddenfield
}

回答by AEMLoviji

yes you are right. You must do it on ItemDateBound. Check It must work

你是对的。您必须在 ItemDateBound 上执行此操作。检查它必须工作

回答by Brissles

Try adding

尝试添加

OnRowDataBound="addLabelsWhereNeeded"

to your GridView. Then cast the control in the corresponding cell to a HiddenField to grab the value:

到您的 GridView。然后将对应单元格中的控件强制转换为 HiddenField 以获取值:

protected void addLabelsWhereNeeded(object sender, GridViewRowEventArgs e)
{
    HiddenField hf = e.Row.Cells[0].Controls[1] as HiddenField;
    String theValue = hf.Value;
}

assuming you've defined your GridView as:

假设您已将 GridView 定义为:

<asp:GridView runat="server" ID="gv" OnRowDataBound="addLabelsWhereNeeded"> 
    <Columns>
        <asp:TemplateField> 
          <ItemTemplate>
                <%--your hidden field--%>
          </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 

Just make sure you are indexing the correct cell and correct control within that cell.

只需确保您正在索引正确的单元格并在该单元格中正确控制。

回答by citronas

I do quite see what you want to achieve with this private field while databinding? In the RowDataBound Event you can access the whole data item, so there is no need for the use of a hidden value.

我很清楚你想在数据绑定时用这个私有字段实现什么?在 RowDataBound 事件中,您可以访问整个数据项,因此无需使用隐藏值。

Pseudocode:

伪代码:

protected void Grid1_RowDataBound(object sender, GridViewRowEventArgs)
{
 if(e.RowType == RowType.DataRow)
 {

 }
}

Set a Breakpoint into if clause and use quickwatch to see how you need to cast the DataItem that is currently bound to gain full access to all properties, even if they aren't bound to any control.

在 if 子句中设置断点并使用 quickwatch 来查看您需要如何转换当前绑定的 DataItem 以获得对所有属性的完全访问权限,即使它们没有绑定到任何控件。