ASP:Panel / UserControl / UpdatePanel 组合出现奇怪的新错误

发布于 2024-08-24 19:16:18 字数 967 浏览 5 评论 0原文

我将在这里尽可能地进行描述。我们有一些代码曾经可以工作,但现在如果没有更改,就不能工作了。我已经隔离了这个问题,所以情况是这样的:

我们有一个 ASPX 页面,上面有 2 个 asp:Panel 控件(Panel1 和 Panel2)。在每个面板中,都有一个 WebUserControl(Panel1 包含 UserControl1,Panel2 包含 UserControl2)。此页面上有 AJAX 工具包 Scriptmanager,仅此而已。当用户第一次访问页面时,Panel1 显示,Panel2 隐藏。

在 UserControl1 中,有一个 asp:Panel、一个包含 asp:UpdatePanel 的 asp:Label 以及其中一个 asp:Button。在后面的代码中,有一个委托和一个引发它的事件 - 当您单击按钮时,会触发该事件。在包含 UserControl 的 ASPX 页面中有一个针对该事件的处理程序。

在Panel2中有一个asp:UpdatePanel、asp:Panel和asp:Label

所以在ASPX页面中,当你点击UserControl1中的按钮时,事件被处理。然后,该事件处理程序隐藏 Panel1 并显示 Panel2。至少过去是这样,但现在不会发生。

总结一下 - 第一次访问的 ASPX 页面显示 Panel1 和 UserControl1。在 UserControl1 中有一个按钮,用户单击该按钮会在 ASPX 页面中引发事件,这应该隐藏 Panel1 并显示 Panel2。

如果您从 UserControl1 和 UserControl2 中删除 UpdatePanel,则代码可以正常工作。

显然,我们的“现实生活”代码更复杂 - 但这个小例子显示了问题所在。

以下是包含该问题的 zip:http://test.wikisaber.es/testajax.zip

I'll try to be as descriptive as possible here. We have some code that was working and now, without being changed, is not. I've isolated the issue, so here is the situation:

We have an ASPX page, and on it there are 2 asp:Panel controls(Panel1 and Panel2). In each of these panels, there is a single WebUserControl (Panel1 contains UserControl1, Panel2 contains UserControl2). There is the AJAX toolkit Scriptmanager on this page, and that is all. When the User first visits the page Panel1 is shown and Panel2 is hidden.

In UserControl1, there is an asp:Panel, a asp:Label containing an asp:UpdatePanel and inside it an asp:Button. In the code behind, there is a delegate and an event to raise it - when you click the button this event is fired. There is a handler for this event in the ASPX page containing the UserControl.

In Panel2 there is an asp:UpdatePanel, asp:Panel and asp:Label

So in the ASPX page, when you click the button in UserControl1, the event is handled. This event handler then hides Panel1 and shows Panel2. At least that is what USED to happen, but this does not happen now.

So to summarise - ASPX page on first visit shows Panel1 with UserControl1. In UserControl1 there is button, User clicks this which raises event in ASPX page, this is supposed to hide Panel1 and show Panel2.

If you remove the UpdatePanels out of UserControl1 and UserControl2, the code works.

Obviously, our 'real life' code here is more complicated - but this small example shows where the problem is.

Here is a zip containing the issue: http://test.wikisaber.es/testajax.zip

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫瑾少女 2024-08-31 19:16:18

仅更新更新面板的内容。因此,您需要从 panel1 和 panel2 中删除更新面板,然后将 panel1 和 panel2 放置在同一个更新面板中。

编辑:从您的示例中,如果我们扩展default.aspx以包含TestUserControl1,我们会得到类似的结果:

         <asp:Panel ID="panelUC1" runat="server" Visible="false">
            <asp:Panel ID="panelUC1" runat="server">
              <div>
                <asp:Label ID="Label1" runat="server" Text="User Control 1...." />
              </div>
             <asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
                 <ContentTemplate>
                    <!-- only the content in from here -->
                    <div>
                      <asp:Button ID="buttonNext" runat="server" OnClick="buttonNext_Click" Text="Click" />
                   </div>  
                    <!-- to here will change when you click on buttonNext -->                       
                </ContentTemplate>
             </asp:UpdatePanel>
           </asp:Panel>
        </asp:Panel>

        <asp:Panel ID="panelUC2" runat="server" Visible="false">
            <tuc2:testUserControl2 ID="testUserControl2" runat="server" />
        </asp:Panel>

当您单击buttonNext时,页面将回发并完成完整的asp.net生命周期,但是控制更新面板的javascript将仅适用对更新面板的 contenttemplate 所做的更改,该更改向浏览器中已存在的 dom 发出请求。尝试在单击事件中重置buttonNext 的文本,看看我想说什么。

我找到这篇文章 http://msdn更新面板和用户控件上的 .microsoft.com/en-us/library/Bb398780(en-us,VS.90).aspx

Only the content of the update panel will be updated. So you need to remove your update panels from panel1 and panel2 and then place panel1 and panel2 in the same update panel.

Edit: From your sample if we expanded the default.aspx to include TestUserControl1 we get something like:

         <asp:Panel ID="panelUC1" runat="server" Visible="false">
            <asp:Panel ID="panelUC1" runat="server">
              <div>
                <asp:Label ID="Label1" runat="server" Text="User Control 1...." />
              </div>
             <asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
                 <ContentTemplate>
                    <!-- only the content in from here -->
                    <div>
                      <asp:Button ID="buttonNext" runat="server" OnClick="buttonNext_Click" Text="Click" />
                   </div>  
                    <!-- to here will change when you click on buttonNext -->                       
                </ContentTemplate>
             </asp:UpdatePanel>
           </asp:Panel>
        </asp:Panel>

        <asp:Panel ID="panelUC2" runat="server" Visible="false">
            <tuc2:testUserControl2 ID="testUserControl2" runat="server" />
        </asp:Panel>

When you click on buttonNext the page will postback and go throught the full asp.net lifecycle, however the javascript that controls the update panel will only apply the changes made to contenttemplate of the update panel that made the request to the dom already present in the browser. Try resetting the text of buttonNext in the click event and see what I'm trying to say.

I found this article http://msdn.microsoft.com/en-us/library/Bb398780(en-us,VS.90).aspx on update panels and usercontrols.

相对绾红妆 2024-08-31 19:16:18

您的按钮仍然位于更新面板内,并且在 3.5 中,更新面板始终将其内部的事件视为触发器,除非您告诉它不要这样做。这绝对是 3.0 中可用内容的变化,但自 3.5 可用以来它没有改变,至少我不知道。

因此,您的页面所做的就是触发按钮,作为 UpdatePanel 部分呈现的一部分。由于只是部分渲染,所以只更新了UpdatePanel内部的内容,页面逻辑并不在UpdatePanel内部。

要解决此问题,您需要做的就是将此代码添加到 UpdatePanel 中:

<Triggers>
    <asp:PostBackTrigger ControlID="buttonNext" />
</Triggers>

这将强制它进行回发,并使您的第二个面板显示。

Your button is still inside the update panel, and with 3.5, the update panel does always treat events inside it as triggers unless you tell it not to. This most definitely is a change from what was available in 3.0, but it has not changed since 3.5 became available, at least not that I am aware of.

Because of this, what your page was doing is fire the button as part of the partial rendering of the UpdatePanel. Since it was only partially rendering, only the contents inside the UpdatePanel got updated, and the page logic is not inside the UpdatePanel.

To fix this, all you need to do is add this code to your UpdatePanel:

<Triggers>
    <asp:PostBackTrigger ControlID="buttonNext" />
</Triggers>

This will force it to do a post back, and will get your second panel to show up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文