views:

2508

answers:

1

I currently have a gridview that has an asp:ButtonField as one of the columns. The event handler for the command extracts the row id of the gridview from the command argument and uses that to perform some logic. I now need to switch to using a template field for this column, and want to do something like this:

<asp:TemplateField HeaderText="Action">
    <ItemStyle HorizontalAlign="Center" />
        <ItemTemplate>
            <asp:LinkButton CommandName="myaction" CommandArgument="<%#Eval("id")%>" Text="do action" runat="server"/>
        </ItemTemplate>
 </asp:TemplateField>

My problem is with the CommandArgument attribute - I don't know how to get it to be the row id from the GridView. Eval("id") doesn't work - does anyone know what the name of the row id property is? Or a better way to do this?

+2  A: 

Check out this document on Microsoft's website.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

You don't need to bind the row id. The command argument is used for event arguments as opposed to row arguments... if that makes sense.

I guess what I'm trying to say is the row id is implicit. When you access the event CommandArgs it should be for something specific to the event you're trying to raise.

i.e. If you wanted to page, you'd have "next", "prev", "first", "last" or a page number in the CommandArgument.

Say you have a "Delete" Button or ImageButton. It's clicked by the user; the delete event is received on the postback and bubbled up from the row to the grid with the whole row object passed as an argument. The grid then deletes the row as if it were a single control in a control collection - because that's what it is, the table is a collection of table rows.

BenAlabaster