ASP.NET + GridView + CommandField as TemplateField
Asked Answered
A

3

10

I have a GridView. My GridView has a column that is contains an "Options" column. This column includes the traditional CommandField options (edit, delete, etc.). I have the code setup to work when a CommandField is used. However, I need to do some custom formatting so I needed to convert the CommandField to a TemplateField.

My question is, how do I trigger the OnRowCommand, OnRowEditing, OnRowDeleting, and OnRowUpdating events from the various LinkButton elements in my TemplateField?

Thank you!

Agna answered 30/10, 2009 at 0:43 Comment(0)
P
24

All you have to do is set the CommandName property of the LinkButton inside of your template column to 'Edit' for editing, 'Delete' for deleting and 'Update' for updating. This will trigger the GridView RowEditing, RowDeleting and RowUpdating events respectively. To trigger the RowCommand event you need to set the OnRowCommand property of your GridView control.

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"
    OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
    OnRowUpdating="GridView1_RowUpdating">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <!--To fire the OnRowEditing event.-->
            <asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit" 
                Text="Edit">
            </asp:LinkButton>
            <!--To fire the OnRowDeleting event.-->
            <asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete" 
                Text="Delete">
            </asp:LinkButton>
            <!--To fire the OnRowUpdating event.-->
            <asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update" 
                Text="Update">
            </asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>    
</asp:GridView>
Phantasmagoria answered 30/10, 2009 at 3:26 Comment(0)
R
13

I had the same problem.

For edit, I did the following:

        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="EditButton"
                                runat="server"
                                CommandName="Edit" 
                                Text="Edit" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:LinkButton ID="UpdateButton"
                                runat="server"
                                CommandName="Update"
                                Text="Update" />&nbsp;
                <asp:LinkButton ID="Cancel"
                                runat="server"
                                CommandName="Cancel"
                                Text="Cancel" />
            </EditItemTemplate>
        </asp:TemplateField>

This allows for the showing/hiding of the update and cancel buttons.

As for delete, I used the following:

    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="DeleteButton"
                            Text="Delete"
                            CommandName="Delete" 
                            runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
Robi answered 7/9, 2010 at 22:16 Comment(0)
I
1

click on Columns in properties, add CommandField(Edit,update,Cancel) and Click on the "Convert this field to templateField"

Swich to Source and automatically going to add a code.

Indigenous answered 11/4, 2012 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.