views:

712

answers:

3

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

A: 

Hi Matt,

is it possible to add another ID column, as a unique representative for a record(row) ? That particular ID column can be set with style: display=none in order to hide it, but still the ID is inside the HTML form.

and then you can retrieve the value of that particular ID column to be processed further.

hope this helps,

hadi

hadi teo
+1  A: 

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"];
Dan
+1  A: 

You can use a template field with the commandArgument equal to the the ID of the record/row.

<ItemTemplate>
  <asp:Button ID="Button1" runat="server" CausesValidation="false" 
      CommandName="SomeCommand" Text="Button" CommandArgument='<%# Eval("SomeID") %'></asp:LinkButton>
</ItemTemplate>

Then, in the row command handling event you can get that value by e.CommandArgument

ScottE