user1760623
user1760623

Reputation:

Accessing dynamically created HiddenField value in Code Behind

I have a HiddenField that I'm creating on a GridView's OnRowDataBound event:

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.

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.

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. 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.

Upvotes: 0

Views: 1898

Answers (1)

Nathan Prather
Nathan Prather

Reputation: 2106

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          
   }            
}

Upvotes: 1

Related Questions