views:

197

answers:

2

If I have this <asp:ButtonField runat="server" DataTextField="Name" CommandName="GetName"></asp:ButonField> Within a GridView. Is there any way to retrieve the DataTextField (Name) from the OnRowCommand method?

<asp:GridView ID="GridView1" runat="server" 
 AllowPaging="True" AutoGenerateColumns="False" 
 DataSourceID="ObjectDataSource_Names" 
 DataKeyNames="ID,ModuleId" OnRowCommand="ChangeName">

Or alternatively, is there any way to make CommandName into an attribute where the command is dynamically entered based on given data, similar to the difference between DataTextField vs TextField.

A: 

I added <asp:BoundField DataField="Name" /> Then you can retrieve it using the same method here: GridView.onRowCommand Method

All I need to do now is find out a way to hide the field. If anyone knows how to, please let me know. Visible="false" gets rid of the field all together...

Matt
In your BoundField add ControlStyle-CssClass="someClass". Put someClass in your page or style sheet and set its display: none;.
Joel Etherton
A: 

Not really sure if this is the best way, but it works:

Make your column a templatefield and bind the name value to a asp:hiddenfield control:

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