4

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?

DreamTeK
  • 32,537
  • 27
  • 112
  • 171
  • 1
    Could you try setting `OnClick="btnSave_Click"` in your markup? – hardkoded Sep 18 '17 at 11:21
  • https://stackoverflow.com/questions/25075538/button-click-not-working-inside-update-panel – Nikolaos Polygenis Sep 18 '17 at 12:02
  • @NikolaosPolygenis I appreciate any help offered but I outlined in my question that the answer you posted wouldn't work and isn't even correct. – DreamTeK Sep 18 '17 at 12:45
  • At a glance: after postback if `upPan` isn't updated (`upPan.Update()`) so controls loose their event listeners. – Hugo Yates Sep 18 '17 at 12:54
  • @HugoYates Correct me if I am wrong but don't postbacks triggered from within the update panel automatically cause the panel to be updated? – DreamTeK Sep 18 '17 at 13:02
  • @kblok Yes tried but the effect is the same. – DreamTeK Sep 18 '17 at 13:16
  • @Obsidian yes I believe that correct, child controls of the UpdatePanel should trigger it to update. I'd check the JS console on your browser, usually when PostBack breaks it's because something has caused JavaScript to blow a gasket. – Hugo Yates Sep 18 '17 at 13:47
  • @HugoYates Yes this was my first conclusion but I cannot understand why simply changing the buttons to linkbuttons fixes the problem. – DreamTeK Sep 18 '17 at 13:54

1 Answers1

2

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
Hugo Yates
  • 2,081
  • 2
  • 26
  • 24