0

Despite the fact that I have placed my asp:Button inside an UpdatePanel, it is still triggering a postback on the full page the first time it's clicked. Also, the OnClick event isn't being caught the first time I click the button either, but every single time after that everything works fine.

Any ideas what could be causing this problem? See the code below.

(In my Site.Master file)

<asp:ScriptManager runat="server" AjaxFrameworkMode="Enabled" EnablePartialRendering="true" ValidateRequestMode="Disabled">
</asp:ScriptManager>

(In my actual webpage)

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Editor.aspx.cs" Inherits="Technology.WebForm1"
validateRequest="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<textarea id="htmlTexarea" runat= "server" style="height: 90%"></textarea>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="testBtn" EventName="Click" />
    </Triggers>
    <ContentTemplate>
        <asp:Button ID="testBtn" style="" runat="server" ClientIDMode="Static" OnClick="testBtn_Click" UseSubmitBehavior="false" />
    </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

My C# codebehind is:

protected void Page_Init(object sender, EventArgs e) {
        testBtn.Click += testBtn_Click;
    }
protected void Page_Load(object sender, EventArgs e)
    {
    }
protected void testBtn_Click(object sender, EventArgs e)
    {
        String test = "Helloworld";
    }

Is there anything I've left out or have done wrong?

EDIT: I added the following to the C# code behind:

 protected void Page_Load(object sender, EventArgs e)
    {
        //Should return POST, returns GET on first click
        String test = Request.HttpMethod;
        if (!IsPostBack)
        {
            //stops here first time
            String hello = "Hello world";
        }
        else { 
            //should stop here
            String hello = "Hello world";
        }
    }

The first time I click the button the server is getting a GET request and IsPostBack is returning false, without changing anything every other click sends a POST request and IsPostBack is true. Anyone know what could be causing this?

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
EoinM
  • 165
  • 3
  • 15
  • Most probably this is because of the `UseSubmitBehavior=false`. – Wiktor Zychla Jul 28 '14 at 10:44
  • Can you try it by removing the triggers section. Just remove the triggers section and try once. I never use the triggers section for a simple button click in an updatePanel. Hence i ask. Also remove the extra Click event added in Pre_init. Not sure why that is required – trelston Jul 28 '14 at 12:20
  • @WiktorZychla Nope, just took that line out and it's still not working, thanks though – EoinM Jul 28 '14 at 12:21
  • @trelston Removed the triggers section and commented out the line where I add the event handler just there but I'm still having the same problem, first click won't work, every other click afterwards does – EoinM Jul 28 '14 at 12:29
  • Is there another button with id as testBtn on the same page? Also i dont have AjaxFrameworkMode="Enabled" EnablePartialRendering="true" ValidateRequestMode="Disabled". Try removing them and check. – trelston Jul 28 '14 at 12:47
  • @trelston No there isn't, there was another button with a different ID but I removed that just to be sure. I removed all of those from the script manager and the issue still persists. – EoinM Jul 28 '14 at 13:29
  • @trelston Could you please vote this up (provided you don't think this is a poorly asked question) so that someone else might see it? – EoinM Jul 29 '14 at 16:00

1 Answers1

2

The problem was being caused by the fact that I was going from another page to this page using Server.Transfer(...), I'm not entirely sure how but this was affecting the POST request sent by the page the first time but once the page reloaded itself after the request everything worked. In my master page I changed the code to Response.Redirect(...) and it now works perfectly. Apologies if this isn't the clearest explanation but to be perfectly honest I'm not quite sure why this solved the problem, if anyone could clarify what was going on in the comments I'd really appreciate it.

EoinM
  • 165
  • 3
  • 15