[英]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();
}
}
}
用户界面如下:
(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方法。 在用户控件中公开一个Event,在使用该用户控件的页面中实现并调用第二个UpdatePanel的Update方法。
为了明确起见,UpdatePanelTest.ascx.cs中Button1 click事件的代码需要更改为以下内容:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.