C# ASP.NET 页面不应在提交时回发整个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9334289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:56:54  来源:igfitidea点击:

ASP.NET page should not post back the whole page on Submit

c#asp.netajaxvb.net

提问by Etienne

I have an ASP.NET page with a Submit button. When I click the button my code runs and then the page reload/refresh....the whole page gets posted back.

我有一个带有提交按钮的 ASP.NET 页面。当我单击按钮时,我的代码运行,然后页面重新加载/刷新....整个页面被回传。

What must I use or do to prevent the page from reload? AJAX? How would this be done?

我必须使用或做什么来防止页面重新加载?阿贾克斯?这将如何完成?

Code examples would be great!

代码示例会很棒!

回答by Marc

If you want to avoid that your code is run again you can do in your page_load event

如果您想避免再次运行您的代码,您可以在 page_load 事件中执行

If(!page.IsPostBack())
{
  your code here
}

and ajax to avoid reload in jquery with the example here

和 ajax 以避免在 jquery 中重新加载 这里的示例

or with asp ajax panel

或使用 asp ajax 面板

回答by Jonathan Egerton

If you don't want the page to reload, you should take a look at the UpdatePanel Control: http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx

如果您不想重新加载页面,则应查看 UpdatePanel 控件:http: //msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx

回答by Pankaj

Check the below code with AJAX and Without AJAX

使用 AJAX 和无 AJAX 检查以下代码

Case 1 With AJAX

案例 1 使用 AJAX

In this we added following controls

在此我们添加了以下控件

1. Script Manager
2. Ajax Update Panel with Trigger and Content Template
3. Button

In this case, you will notice that on clicking the button, the controls which are outside the Update Panelwill not be updated. That means controls inside the Update Panelwill be updated on clicking the button. That means complete page will not be refreshed. I hope this fulfill your requirement....

在这种情况下,您会注意到单击该按钮时,Update Panel不会更新位于 之外的控件。这意味着Update Panel将在单击按钮时更新内部的控件。这意味着不会刷新完整的页面。我希望这能满足您的要求......



Output

输出

Date outside the update panel will not be updated and date inside the update panel will only be updated because it is inside the update panel so complete page will not be refreshed.

更新面板外的日期不会更新,更新面板内的日期只会更新,因为它在更新面板内,因此不会刷新完整的页面。



Source Code

源代码

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%=DateTime.Now %>
        <br />
        <asp:ScriptManager ID="scr" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="Upd" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <%=DateTime.Now %>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btn" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:Button ID="btn" runat="server" Text="Click Me" />
    </div>
    </form>
</body>
</html>


Case 2 Without AJAX

案例 2 没有 AJAX

The below case will also update the date but this time page will be refreshed completely due to absence of Update Panel<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

以下情况也会更新日期,但由于没有Update Panel<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%=DateTime.Now %>
        <asp:Button ID="btn" runat="server" Text="Click Me" />
    </div>
    </form>
</body>
</html>


Hope this article will help you understand the quick basics

希望这篇文章能帮助您了解快速基础知识