首页 文章

按钮单击会在第一次单击时触发整页回发

提问于
浏览
0

尽管事实上我已将我的asp:Button放在UpdatePanel中,但它仍然在第一次点击时在整页上触发回发 . 此外,OnClick事件在我第一次单击按钮时也没有被捕获,但每次之后一切都正常 .

什么可能导致这个问题的想法?请参阅下面的代码 .

(在我的Site.Master文件中)

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

(在我的实际网页中)

<%@ 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>

我的C#代码隐藏是:

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";
    }

有什么我遗漏或做错了吗?

编辑:我在后面的C#代码中添加了以下内容:

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";
        }
    }

第一次单击按钮时,服务器正在获取GET请求并且 IsPostBack 返回false,而没有更改任何其他任何单击发送POST请求且 IsPostBack 为真 . 有人知道是什么原因引起的吗?

1 回答

  • 0

    问题是由于我使用 Server.Transfer(...) 从另一个页面转到此页面这一事实,我不完全确定如何但这会影响第一次页面发送的POST请求但是一旦页面重新加载后请求一切正常 . 在我的母版页中,我将代码更改为 Response.Redirect(...) ,现在它完美无缺 . 抱歉,如果这不是't the clearest explanation but to be perfectly honest I'我不太清楚为什么这解决了问题,如果有人能澄清评论中发生了什么,我真的很感激 .

相关问题