Converting asp: ButtonField to asp: TemplateField in GridView control

I currently have a gridview that has an asp: ButtonField as one of the columns. The event handler for the command retrieves the row id of the gridview from the command argument and uses that to execute some logic. Now I need to switch to using a template field for that 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 is the name of the row id property? Or is there a better way to do this?

+1


source to share


2 answers


Check out this document on the Microsoft website.

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

You don't need to bind the string id. The command argument is used for argument arguments, not string arguments ... if that makes sense.



I'm guessing what I'm trying to say is that the string id is implicit. When you access the CommandArgs event, it should be for something specific to the event you are trying to raise.

i.e. If you want to go to a page, you must have "next", "previous", "first", "last" or page number in the CommandArgument.

Say you have a Delete button or ImageButton. He clicked by the user; a delete event is received on postback and bubbles from line to grid with an integer line object passed as an argument. The grid then removes the row as if it were a single control in a collection of controls - since that's what it is, a table is a collection of table rows.

+3


source


CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>"

      



+1


source







All Articles