Sys.WebForms.PageRequestManager.getInstance().add_beginRequest is causing a button click event not to fire.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string enableOnTimeoutScript ="<script type='text/javascript'>Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(" +"function (sender, args) { if (args.get_error()) { "; int buttonIndex = 0; foreach (Button button in allButtons) { enableOnTimeoutScript += string.Format("var button{0} = document.getElementById('{1}'); " +"if (button{0} != null) button{0}.disabled = true; ", buttonIndex++, button.ClientID); } enableOnTimeoutScript += string.Format("var validator0 = document.getElementById('{0}'); " +"if (validator0 != null) {{ validator0.style.visibility = 'visible'; " +"validator0.innerHTML = args.get_error().message; }} ", customValidator.ClientID); enableOnTimeoutScript += "args.set_errorHandled(true); } });</script>"; Literal enableOnTimeoutLiteral = new Literal(); enableOnTimeoutLiteral.Text = enableOnTimeoutScript; panelDocumentNav.Controls.Add(enableOnTimeoutLiteral); } Button saveData = new Button(); saveData.EnableViewState = true; saveData.Click += new EventHandler(dynamicButton_Click); saveData.Text = "SAVE"; }
<asp:Content ID="contentBody" ContentPlaceHolderID="contentPlaceHolderBody" runat="server"><div style="height: 100%; left: 0px; position: absolute; top: 0px; width: 76%;"></div><asp:Panel ID="panelDocumentNav" runat="server" CssClass="panel" Width="24%" Style="height: 100%; position: absolute; right: 0px; top: 0px;"><div class="spacer" style="overflow: auto; height: 100%"><asp:PlaceHolder runat="server" ID="dynamicControls"></asp:PlaceHolder></div><asp:UpdatePanel ID="updatePanelQ" runat="server"><ContentTemplate><div><asp:CustomValidator ID="customValidator" runat="server" CssClass="error" /></div></ContentTemplate></asp:UpdatePanel>
When the page is first loaded and the SAVE button is clicked the dynamicButton_Click event is not fired but a post back occurs b/c the Page_Load method is called but the if (!IsPostBack) is false so code within if statement is not executed. The page is reloaded.
If I click the button again the dynamicButtonClick event is fired.
If I comment out the code in the if (!IsPostBack) the dynamicButton_Click event is fired on the first click.
Why is the event not being fired on the first click?