111,098
社区成员




<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
using System;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
System.Web.UI.Page page = GetPage(this) as System.Web.UI.Page;
Button btn = page.FindControl("Button1") as Button;
Type t = btn.GetType();
MethodInfo mi = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(btn, new object[] { e });
}
private Control GetPage(Control ctrl)
{
if (ctrl is System.Web.UI.Page)
return ctrl;
else if (ctrl.Parent != null)
return GetPage(ctrl.Parent);
else
return null;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!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:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Control ctrl = this.LoadControl("~/WebUserControl1.ascx");
WebUserControl1 wc = ctrl as WebUserControl1;
this.form1.Controls.Add(wc);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('Text changed!');</script>");
}
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
using System;
namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public delegate void tc(object sender, EventArgs e);
public event tc TextChanged;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (TextChanged != null)
{
TextChanged(sender, e);
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="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>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl11.TextChanged += new WebUserControl1.tc(WebUserControl11_TextChanged);
}
void WebUserControl11_TextChanged(object sender, EventArgs e)
{
Button1_Click(Button1, e);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('Text changed!');</script>");
}
}
}