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?

4

1 回答 1

2

问题是由于我从另一个页面到这个页面使用的事实引起的 Server.Transfer(...),我不完全确定如何但这会影响页面第一次发送的 POST 请求,但是一旦页面在请求后重新加载一切正常。在我的母版页中,我将代码更改为Response.Redirect(...),现在它可以完美运行。抱歉,如果这不是最清楚的解释,但老实说,我不太确定为什么这解决了问题,如果有人能澄清评论中发生的事情,我将不胜感激。

于 2014-07-30T10:09:58.763 回答