简体   繁体   English

ASP.Net AJAX表单导致Page_Load

[英]ASP.Net AJAX form causes Page_Load

I am new to AJAX and I'm trying to create a very small test just updating a label when pressing a button without reloading the page. 我是AJAX的新手,我试图创建一个很小的测试,只是在按下按钮时更新标签,而无需重新加载页面。 The c# method gets called and the label is updated, but the page still seems to reload since the Page_Load is called with IsPostBack=true. 调用c#方法并更新标签,但是由于使用IsPostBack = true调用了Page_Load,因此页面似乎仍在重新加载。 What am I missing here? 我在这里想念什么? Using Visual Studio 2017. 使用Visual Studio 2017。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="Default.css" rel="stylesheet" type="text/css" />

</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="MainScriptManager" runat="server" enablepartialrendering="true" />
        <div class="main">   
            <div>
                <asp:UpdatePanel ID="pnlMain" runat="server">
                    <ContentTemplate>
                        <asp:Label runat="server" ID="lblTest" Text="Inte tryckt" />
                        <asp:Button ID="btnImport" runat="server" class="button" Text="Import" OnClick="btnSelectFile_Click" />
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>


        </div>

    </form>
</body>
</html>

This is the default behavior, even for post back made through UpdatePanel , IsPostBack will be true . 这是默认行为,即使对于通过UpdatePanel进行的回发, IsPostBack也将为true

If you want to differentiate the postback you can check like following. 如果要区分回发,可以按如下所示进行检查。

if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)         
{          
    // Your code when the request is partially posted back         
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM