C# 更新面板中的按钮正在执行完整回发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/654423/
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-04 12:01:17  来源:igfitidea点击:

Button in update panel is doing a full postback?

c#asp.netajaxiis

提问by user48408

I'm following a simple example of how to use the update panel from here (http://www.asp.net/Ajax/Documentation/Live/tutorials/IntroductionUpdatePanel.aspx). Outside the update panel i've another html input control which calls a javascript function which displays an count to the user in an alert box. simple stuff. My problem is that the page behaves differently when running on IIS and on the inbuilt asp.net web server (cassini). Under IIS clicking the button within the update panel causes a full postback and so the count displayed to the user in the js function gets reset after that eachtime. under the inbuilt web server hitting the button inside the update panel behaves how i would expect it to and how i want it to in that it refreshes the label only so that the counter on the client side isn't reset.

我正在按照一个简单的示例说明如何使用此处的更新面板 ( http://www.asp.net/Ajax/Documentation/Live/tutorials/IntroductionUpdatePanel.aspx)。在更新面板之外,我有另一个 html 输入控件,它调用一个 javascript 函数,该函数在警报框中向用户显示计数。简单的东西。我的问题是页面在 IIS 和内置的 asp.net Web 服务器 (cassini) 上运行时的行为不同。在 IIS 下,单击更新面板中的按钮会导致完全回发,因此每次在 js 函数中显示给用户的计数都会重置。在内置的 Web 服务器下点击更新面板内的按钮,它的行为方式是我期望的方式以及我希望它的方式,因为它只会刷新标签,以便客户端的计数器不会被重置。

.net 3.5 is the target framework and I'm running IIS 5.1.

.net 3.5 是目标框架,我运行的是 IIS 5.1。

I've seen posts elsewhere describing the same problem (http://forums.asp.net/t/1169282.aspx)

我在其他地方看到过描述相同问题的帖子(http://forums.asp.net/t/1169282.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>

    <script type="text/javascript">
    var count=0;
    function incrementCounter()
    {
        count ++;
        alert(count);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server" Text="Panel Created"></asp:Label>
            <asp:Button ID="Button1" runat="server" Text="Button"     onclick="Button1_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>

    <input type="button" id="Button2" value="JS Clicker" onclick="incrementCounter();" />
    </form>
</body>
</html>

Update:

更新:

Thanks Crossbrowser for your answer. My reply will take up to much room in the Add Comment window. Ok, so following this simple example here (http://www.asp.net/Ajax/Documentation/Live/tutorials/IntroductionUpdatePanel.aspx) you can see that the update mode is not set to conditional so I've reflected those changes. However my problem still persists. That is that the page when running on IIS causes a full postback. i.e. the progress bar in you browser loads, the screen flickers, client side count that i was maintaining is lost. Running the code on the inbuilt asp.net webserver does not. That is the crux of my problem. I've come across this problem by others (http://forums.asp.net/t/1169282.aspx).

感谢 Crossbrowser 的回答。我的回复将在“添加评论”窗口中占用大量空间。好的,按照这里的这个简单示例(http://www.asp.net/Ajax/Documentation/Live/tutorials/IntroductionUpdatePanel.aspx),您可以看到更新模式未设置为有条件的,因此我已反映了这些更改. 但是我的问题仍然存在。那就是在 IIS 上运行时的页面会导致完整的回发。即浏览器中的进度条加载,屏幕闪烁,我维护的客户端计数丢失。在内置的 asp.net 网络服务器上运行代码不会。这是我问题的症结所在。我遇到过其他人的这个问题(http://forums.asp.net/t/1169282.aspx)。

So my question is what is different when running on IIS compared to the inbuilt asp.net one?

所以我的问题是在 IIS 上运行与内置的 asp.net 相比有什么不同?

Updated Code:

更新代码:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>

    <script type="text/javascript">
    var count=0;
    function incrementCounter()
    {
        count ++;
        alert(count);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server" Text="Panel Created"></asp:Label>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>

    <input type="button" id="Button2" value="JS Clicker" onclick="incrementCounter();" />
    </form>


</body>
</html>

采纳答案by Bryan Sebastian

Since you are using the .NET Framework 3.5 I will assume you are using Visual Studio 2008, and you say you are targeting IIS 5.1 for the production platform.

由于您使用的是 .NET Framework 3.5,我假设您使用的是 Visual Studio 2008,并且您说您的目标是 IIS 5.1 作为生产平台。

The local web server that is part of Visual Studio 2008 is based off of IIS 6/7 architecture, not IIS 5. So, to answer your question of what is different with IIS compared to the local web server... unfortunately, in this case, you are mixing apples and oranges.

作为 Visual Studio 2008 一部分的本地 Web 服务器基于 IIS 6/7 体系结构,而不是 IIS 5。因此,回答您的问题,即 IIS 与本地 Web 服务器相比有何不同......不幸的是,在此案例,你正在混合苹果和橙子。

Are you restricted to IIS 5.1?... ie client mandate or some other reason. If you are not, and you are developing with Visual Studio 2008 (.NET Framework 3.5) you really should be using IIS7 (or at least 6) as you would most likely not have this problem.

您是否仅限于 IIS 5.1?...即客户端授权或其他一些原因。如果不是,并且您正在使用 Visual Studio 2008 (.NET Framework 3.5) 进行开发,那么您确实应该使用 IIS7(或至少 6),因为您很可能不会遇到此问题。

Again, IIS7 may not be an option for you.

同样,IIS7 可能不适合您。

回答by Thomas Favrbo

Did you try using the <asp:Button runat="server">element instead of the HTML <input>element?

您是否尝试使用<asp:Button runat="server">元素而不是 HTML<input>元素?

回答by nickytonline

Have you tried using a trigger? e.g.

您是否尝试过使用触发器?例如

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server" Text="Panel Created"></asp:Label>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Panel Refresh" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Label1" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>

回答by mbillard

Since your UpdatePanel's UpdateModeis set to conditional you have to specify a trigger.

由于您的 UpdatePanel 的UpdateMode设置为条件,您必须指定一个触发器。

Alternatively, you could define the property ChildrenAsTriggersto true.

或者,您可以将ChildrenAsTriggers属性定义为true

UpdatePanel.UpdateMode reference

UpdatePanel.UpdateMode 参考

回答by WakeUpScreaming

In IIS Manager, check the "Pages and Controls" settings for your site. Specifically, the View State and Settings sections. Those look like they could affect how your page interacts with the server and when.

在 IIS 管理器中,检查站点的“页面和控件”设置。具体来说,查看状态和设置部分。那些看起来可能会影响您的页面与服务器交互的方式和时间。