简体   繁体   English

updatepanel更新整个页面而不仅仅是contenttemplate

[英]updatepanel updating whole page instead of just the contenttemplate

                        <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2"  ClientIDMode="AutoID" %>
                        <asp:Content ID="Content1" ContentPlaceHolderID="head"        Runat="server">
                        </asp:Content>

                        <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="server">

                        <% 
                        foreach (var item in AllSales)
                        {
         //Here i have just set a breakpoint to see if it loops the AllSales list when I press the update button
                        }
                        %>

                        <asp:UpdatePanel runat="server" ID="UpdatePanel1">
                        <ContentTemplate>

                        <p>Update Panel: DateTime.Now: <%= DateTime.Now.ToString() %></p>
                        <asp:Button runat="server" ID="Submit" Text="Update" />

                        </ContentTemplate>

                        </asp:UpdatePanel>

                        </asp:Content> 

The script manager code is in the masterpage : 脚本管理器代码位于母版页中:

    <asp:ScriptManager ID="ScriptManager1" runat="server" />

    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

    </asp:ContentPlaceHolder>

Problem here is that everytime i click the Update button it loads the page again and loops the "AllSales" list , i want to only update a section and not have to do unnecessary loops. 这里的问题是每次我点击Update按钮它再次加载页面并循环“AllSales”列表,我想只更新一个部分而不必做不必要的循环。

Here is the fun part : If i remove the masterpage, it works ! 这是有趣的部分: 如果我删除母版页,它的工作原理! But with the masterpage, it dont , why?! 但是随着主页,它不,为什么?!

You need to use a server control to initiate the update rather than a normal html input button. 您需要使用服务器控件来启动更新,而不是普通的html输入按钮。 Try using: 尝试使用:

<asp:Button ID="ActivitySubmit" Text="Submit" runat="server" />

Set the UpdateMode mode property to Conditional. UpdateMode模式属性设置为Conditional。 The default value for UpdateMode is Always , If the UpdateMode property is set to Always, the UpdatePanel control's content is updated on every postback that originates from anywhere on the page, reference UpdateMode的默认值为Always ,如果UpdateMode属性设置为Always,UpdatePanel控件的内容将在源自页面上任何位置的每个回发更新, 引用

Edit: You are using input type="submit" which probably causing the post back replace it with asp:Button to get the ajax call to work. 编辑:您正在使用输入类型=“提交”,这可能导致帖子用asp:Button替换它以使ajax调用工作。

The reason for such behavior is that you use plain html submit button instead of ASP.NET server button. 这种行为的原因是您使用普通的html提交按钮而不是ASP.NET服务器按钮。 Thus page submitted to server without involving ASP.NET Ajax functionality. 因此页面提交给服务器而不涉及ASP.NET Ajax功能。 Replace ActivitySubmit button with asp:Button control 用asp:Button控件替换ActivitySubmit按钮

I have tested the same code with only change in the button type, and it refreshes only the update panel content. 我已经测试了相同的代码,只更改了按钮类型,并且只刷新了更新面板内容。 The problem is your site is targeting .NET 3.0 but you need to target at least to 3.5. 问题是您的网站的目标是.NET 3.0,但您需要至少达到3.5。 I have tested on 3.0, it does not work but on 3.5 and 4.0, it works fine. 我已经在3.0上测试过,它不起作用,但在3.5和4.0上,它运行正常。 So the easier and safer solution is to target 3.5 onwards. 因此,更容易和更安全的解决方案是以3.5为目标。 But If you want to use .NET 3.0, I will try to find a workaround. 但是如果你想使用.NET 3.0,我会尝试找到一种解决方法。 Please let me know. 请告诉我。

I tried to recreate your problem but it is working fine in my case. 我试图重新创建你的问题,但它在我的情况下工作正常。 Try creating a new .aspx file and paste the following: 尝试创建一个新的.aspx文件并粘贴以下内容:

<!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>
        <p>Outside UpdatePanel</p>
        <asp:ScriptManager runat="server" ID="ScriptManager1">
        </asp:ScriptManager>
        <asp:UpdatePanel runat="server" ID="UpdatePanel1">
            <ContentTemplate>
                <p>DateTime.Now: <%= DateTime.Now.ToString() %></p>
                <asp:Button runat="server" ID="SubmitButton" Text="Go" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

This was tested in VS2010 using .NET 4.0 in an Empty Web Application. 这是在VS2010中使用.NET 4.0在空Web应用程序中测试的。 Does this work for you as well? 这对你有用吗? I tried the same example using a Master page with a content placeholder. 我使用带有内容占位符的母版页尝试了相同的示例。 This yields the same (properly working) results. 这产生相同(正常工作)的结果。 From this I gather there is something else going on on your page that we're missing. 从此我收集到你的页面上还有一些我们遗漏的东西。 Is there? 在那儿?

[Edit] [编辑]

I made another simple example, this time with a Master page and a Content page. 我做了另一个简单的例子,这次是主页面和内容页面。

Master page: 母版页:

<!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>
        <asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
        <p>Master Page: DateTime.Now: <%= DateTime.Now.ToString() %></p>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Aspx page: Aspx页面:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<p>Update Panel: DateTime.Now: <%= DateTime.Now.ToString() %></p>
<asp:Button runat="server" ID="Submit" Text="Update" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

Again this works without a problem in the same environment as the other example. 同样,这在与其他示例相同的环境中没有问题。 It looks like you'll have to tell me about the .NET framework you're targeting and what else you've got set up in your master page and aspx page, because: 看起来您将不得不告诉我您要定位的.NET框架以及您在母版页和aspx页面中设置的其他内容,因为:

In .Net 3.5 and up this will work. 在.Net 3.5及以上版本中,这将起作用。 In lower versions however, it won't. 然而,在较低版本中,它不会。 I'm afraid I've been unable to figure out how to fix it in lower versions though. 我担心我无法弄清楚如何在较低版本中修复它。 :( :(

在您的Page标记中添加

ClientIDMode="AutoID" 

I think you can try and put the form inside the updatepanel, or you can try and change children as triggers property of the updatepanel like this : 我认为您可以尝试将表单放在updatepanel中,或者您可以尝试将子项更改为updatepanel的触发器属性,如下所示:

<asp:UpdatePanel runat="server" ID="UpdatePanel1" ChildrenAsTriggers="False">

and then add the button as trigger like this 然后像这样添加按钮作为触发器

   ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(SubmitButton);

try addind this part to your UpdatePanel: 尝试将此部分添加到您的UpdatePanel:

  <Triggers>
     <asp:AsyncPostBackTrigger ControlID="Submit" />
  </Triggers>

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

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