更新面板不适用于用户控件

Update panel not working with user control

如果触发器位于更新面板内的用户控件内,我不知道如何在更新面板中获取触发器以更新花药面板中的控件。这是我的测试用例。

Test.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true"
    CodeBehind="Test.aspx.cs" Inherits="MyTestApp.Test" %>

<%@ Register src="UpdatePanelTest.ascx" tagname="UpdatePanelTest" tagprefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="server">

<div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <uc1:UpdatePanelTest ID="UpdatePanelTest1" runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Label ID="Label2" runat="server" ForeColor="red" />

        </ContentTemplate>

    </asp:UpdatePanel>
    <asp:Button ID="Button3" runat="server" Text="(3) Update Black Time" OnClick="Button3_Click" />
</div>
</asp:Content>

Test.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyTestApp
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Button3_Click(object sender, EventArgs e)
        {
            ((Label)UpdatePanel1.FindControl("UpdatePanelTest1").FindControl("Label1")).Text = DateTime.Now.ToLongTimeString();
        }
    }
}

UpdatePanelTest.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UpdatePanelTest.ascx.cs" Inherits="MyTestApp.UpdatePanelTest" %>
<asp:Label ID="Label1" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="(1) Update Both Times" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="(2) Update Black Time" OnClick="Button2_Click" />

UpdatePanelTest.ascx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyTestApp
{
    public partial class UpdatePanelTest : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToLongTimeString();
            ((Label)Parent.FindControl("UpdatePanel2").FindControl("Label2")).Text = DateTime.Now.ToLongTimeString();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToLongTimeString();
        }
    }
}

UI如下:

(empty first label, which will become a black timestamp)
(1) Update Both Times             (2) Update Black Time
(empty second label, which will become a red timestamp)
(3) Update Black Time

如果我单击位于用户控件内的 (1),只有黑色时间戳会更新,尽管当我对其进行调试时,我可以看到它正确地将文本分配给了红色时间戳。如果我单击 (2),也在用户控件内,它会正确更新黑色时间戳。如果我单击主页上的 (3),它只会更新黑色时间戳,但最终会显示红色时间戳,它具有我单击按钮 (1) 时的时间戳。另一个问题是它会刷新整个页面,这是我试图避免的事情。

当我在另一个 UpdatePanel 中单击用户控件内的控件时,我需要更改什么才能正确更新和显示一个 UpdatePanel 中的控件?我试过另一种方法,单击 UpdatePanel 内的控件尝试更新另一个 UpdatePanel 内的用户控件内的控件,结果相似。

从一个更新面板更新到另一个更新面板可能需要对后者显式调用 Update 方法。在用户控件中公开一个事件,在使用此用户控件的页面中实现并调用第二个 UpdatePanel 的 Update 方法。

明确地说,UpdatePanelTest.ascx.cs 中的 Button1 单击事件代码需要更改为以下内容:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = DateTime.Now.ToLongTimeString();
    ((Label)Parent.FindControl("UpdatePanel2").FindControl("Label2")).Text = DateTime.Now.ToLongTimeString();
    ((UpdatePanel)Parent.FindControl("UpdatePanel2")).Update();
}