简体   繁体   English

在Code Behind中访问动态创建的HiddenField值

[英]Accessing dynamically created HiddenField value in Code Behind

I have a HiddenField that I'm creating on a GridView's OnRowDataBound event: 我有一个我在GridView的OnRowDataBound事件上创建的HiddenField

var hF1 = new HiddenField();
e.Row.Cells[10].Controls.Add(hF1);

In JavaScript, I'm returning a value from a modal and setting the value on the HiddenField for that particular row, "hiddenField" being the ClientID of the HiddenField . 在JavaScript中,我从一个模态返回一个值,并在该特定行的HiddenField上设置值,“hiddenField”是HiddenField的ClientID。

document.getElementById(hiddenField).value = settings;

Now, I have a Button.OnClick event where I need to capture that data for further processing, but I can't determine how I can retreive the data for the specific row. 现在,我有一个Button.OnClick事件,我需要捕获该数据以进行进一步处理,但我无法确定如何检索特定行的数据。

foreach (SPGridViewRow dataRow in gvItems.Rows)
{
    if (dataRow.RowType != DataControlRowType.DataRow) continue;
    var mySettings = dataRow.... ?

How can I capture that value? 我怎样才能捕捉到这个价值? Each value in the row's HiddenField control will be unique to that row. 行的HiddenField控件中的每个值对于该行都是唯一的。 I'm also open to alternative storage of the temporary data held within this field on a per-row basis, just keep in mind that the data is returned from a JavaScript event. 我也愿意以每行为基础对该字段中保存的临时数据进行替代存储,请记住,这些数据是从JavaScript事件返回的。

Instead of creating the hidden field in the code behind I would set it up as a template column like this: 我没有在后面的代码中创建隐藏字段,而是将其设置为模板列,如下所示:

<asp:TemplateField>
   <ItemTemplate>
      <asp:HiddenField ID="myHiddenFieldID" runat="server" 
             Value='<%# Eval("SomeJSCLientID") %>' />
</ItemTemplate>

But that may not be necessary and you may be able to find your hidden control by doing this: 但这可能没有必要,你可以通过这样做找到你隐藏的控件:

foreach (GridViewRow row in grid.Rows)
{
   if (((HiddenField)row.FindControl("myHiddenFieldID")).Value)
   {
    //do your thing          
   }            
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM