R – Equivalence of an asp:HiddenField for a GridView

asp.netdataboundgridviewhidden-fieldtemplatefield

There is no asp:HiddenField that can be used in a GridView so I was wondering what would work similar to this.

My reasoning for wanting this is I have a ButtonField that triggers an OnRowCommand. From there I can figure out which row was selected, but I cannot retrieve the text value from the ButtonField to see the data that was bound to it (through DataTextField).

My solution to this was to have a BoundField and just retrieve the text value from it instead, since I already knew which row was selected. This worked, but I need it to be hidden.

Someone suggested using a HiddenField nested within a TemplateField, but I had troubles retrieving the text value from that HiddenField. Is there some way to access the control within the TemplateField to get the text value of the HiddenField?

If anyone has any suggestions for alternatives that would be great as well.

Thanks,
Matt

Best Answer

You can use the DataKeyNames property on the Gridview.

gridView.DataKeyNames = { "values", "you", "want "};

and then you can access them like this:

gridView.DataKeys[rowId].Values["value"];