Matthew Layton
Matthew Layton

Reputation: 42390

ASP.NET PostBack is not hitting the correct method

Consider the following ASP.NET code:

<asp:UpdatePanel runat="server" ChildrenAsTriggers="true">
            <ContentTemplate>
                <asp:MultiView runat="server" ID="MultiView" ActiveViewIndex="0">
                    <asp:View runat="server">
                    </asp:View>
                    <asp:View runat="server">
                        <p><img alt="Loading..." src="/global/images/ajax-mini-loader.gif" style="vertical-align: middle;" />&nbsp;Loading...</p>
                    </asp:View>
                    <asp:View runat="server">
                        <asp:GridView runat="server" ID="WarrantyView" OnDataBound="WarrantyView_DataBound" AutoGenerateColumns="false" ItemType="WarrantySystem.Data.ServiceCompany">
                            <Columns>
                                <asp:BoundField HeaderText="Name" DataField="Name" />
                                <asp:BoundField HeaderText="Telephone" DataField="Telephone" />
                                <asp:BoundField HeaderText="Email" DataField="Email" />
                                <asp:BoundField HeaderText="Telephone 24/7" DataField="Telephone247" />
                                <asp:BoundField HeaderText="Email 24/7" DataField="Email247" />
                                <asp:TemplateField HeaderText="Actions">
                                    <ItemTemplate>
                                        <asp:Button runat="server" ID="btnEdit" CommandName="Edit" CommandArgument="<%# Item.ID %>" Text="Edit" />
                                        <asp:Button runat="server" ID="btnDelete" CommandName="Delete" CommandArgument="<%# Item.ID %>" Text="Delete" OnCommand="btnDelete_Command"/>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </asp:View>
                    <asp:View runat="server">
                        <p>Your data could no be loaded at this time.</p>
                    </asp:View>
                </asp:MultiView>
                <asp:Timer runat="server" ID="tmrLoadData" Enabled="true" Interval="1" OnTick="tmrLoadData_Tick" />
            </ContentTemplate>
        </asp:UpdatePanel>

When btnDelete is clicked, a postback event occurs to Page_Load, and never hits btnDelete_Command, as it should. I'm lost as to why!

EDIT: The same problem occurs with the edit button...Okay so in the example, there is no OnClick or OnCommand event, but I have just tested this...it does the same thing.

How it works:

EDIT: As per user comments (server-side code command code):

protected void btnDelete_Command(object sender, CommandEventArgs e)
    {
        bool result = this.mgr.DeleteServiceCompany(Int32.Parse(e.CommandArgument.ToString()));
    }

    protected void btnEdit_Command(object sender, CommandEventArgs e)
    {
        Response.Redirect("ServiceCompany.aspx?id=" + e.CommandArgument.ToString());
    }

Upvotes: 1

Views: 770

Answers (1)

Praveen Nambiar
Praveen Nambiar

Reputation: 4892

Use the RowCommand event of GridView to fire your Edit and Delete command.

Add this inside your GridView markup.

OnRowCommand="WarrantyView_RowCommand"

Also change the CommandName text

CommandName="Modify"  // change the name here
CommandName="Remove"  // change the name here

And inside the event

protected void WarrantyView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Modify")
    {
       // your edit logic
    }
    if (e.CommandName == "Remove")
    {
       // your delete logic 
    }
}

I did recommend you to change the name of the CommandName property.
Change Edit to perhaps Modify
Change Delete to perhaps Remove
Reason being they are inbuilt gridview commands

NOTE: Ensure that your ViewState is enabled in all relevant places too. This will not work with ViewState disabled.

Upvotes: 1

Related Questions