Search code examples
c#asp.netvb.netupdatepanelpostback

Asp:Button click event inside update panel not firing


What would cause an asp Button not to fire after a partial update has occured?

<asp:UpdatePanel ID="upPan" runat="server" UpdateMode="Conditional">
  <ContentTemplate>
    <asp:Button ID="btnSave" ClientIDMode="Static" runat="Server" Text="Save" CausesValidation="false" />
  </ContentTemplate>
</asp:UpdatePanel>
  • The first time any button is fired inside the update panel the below routines fire in this order.
  • After a postback has occured triggering the button again causes a postback, both Load and PreRenderComplete events fire but the click event is skipped.

VB

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.PageLoad
  //Runs everytime
End Sub

Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
 //Doesn't fire after first postback.
End Sub

Protected Sub Page_PreRenderComplete(sender As Object, e As EventArgs) Handles Me.PreRenderComplete
  //Runs everytime
End Sub

FAILED RESOLUTIONS

Suggestions to resolve this include:

  • ChildrenAsTriggers= "True" This is already the default behaviour of UpdatePanel and offers no change.
  • <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" /> Again by default child controls of the panel cause an asynchronous postback and declaring triggers is redundant.

SUCCESSFUL RESOLUTION

If I simply change the asp:Button into an asp:LinkButton the issue is resolved.


SUMMARY

The postback is occuring but the click event is being missed when the sender is an asp:Button control.

Can anyone explain what would cause this behaviour?


Solution

  • First up, apologies my answer is in C#. It's also not much of an answer as I can't replicate your issue. The distinction between Buttons and LinkButtons is that a Button uses submit behaviour and a LinkButton uses a javascript postback. You could try putting UseSubmitBehavior="false" on your Button, that'll make it work like a LinkButton.

    Here's my complete test code. Being C# I had to make a few changes as it doesn't have Handles - which maybe a key to the issue as C# and VB handle events slightly differently

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test3.aspx.cs" Inherits="Test3" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
    
                <asp:ScriptManager ID="PageScriptManager" runat="server" />
                <asp:UpdatePanel ID="upPan" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:Button ID="btnSave" ClientIDMode="Static" runat="Server" Text="Button" CausesValidation="false" OnClick="btnSave_Click" />
                        <asp:LinkButton ID="lnkButton" ClientIDMode="Static" runat="Server" Text="Link Button" CausesValidation="false" OnClick="btnSave_Click" />
                        <asp:TextBox ID="txtBox" runat="server" TextMode="MultiLine" Rows="3" />
                    </ContentTemplate>
                </asp:UpdatePanel>
    
            </div>
        </form>
    </body>
    </html>
    

    CodeBehind:

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtBox.Text = "Page_Loaded";
        }
    
        protected void btnSave_Click(object sender, EventArgs e)
        {
            txtBox.Text += "\n" + DateTime.Now.ToString("mm:ss:fff");
        }
    
        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            txtBox.Text += "\nPreRenderComplete";
        }
    }
    

    Clicking the Button (or the LinkButton) works and updates the TextBox everytime:

    Page_Loaded
    55:54:185
    PreRenderComplete