62,046
社区成员
发帖
与我相关
我的任务
分享
protected void Button1_Click(object sender, EventArgs e)
{
if (this.Parent is WebUserControl1)
{
WebUserControl1 wuc = this.Parent as WebUserControl1;
wuc.SetText("Hello");
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="my" TagName="uc2" %>
<my:uc2 runat="server" ID="UC2" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetText(string str)
{
this.TextBox1.Text = str;
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="WebApplication1.WebUserControl2" %>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
public partial class WebUserControl2 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.Parent is UserControl)
{
System.Reflection.MethodInfo mi = this.Parent.GetType().GetMethod("SetText", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (mi != null)
{
mi.Invoke(this.Parent, new object[] { "Hello" });
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="flash.aspx.cs" Inherits="WebApplication1.flash" %>
<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="my" TagName="uc1" %>
<!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>
<my:uc1 ID="UC1" runat="server"></my:uc1>
</div>
</form>
</body>
</html>