ASP.NET用户控件操作ASPX页面(在ASPX页面捕捉用户控件的事件)
一、简单实例
先来个简单点的,首先新建一个 “web用户控件”WebUserControl1.ascx,
在页面上添加一个按钮,并响应Click事件:
[csharp] <asp:Button ID="Button1" runat="server" Text="在页面级的TextBox输入一个随机数" onclick="Button1_Click" />
<asp:Button ID="Button1" runat="server" Text="在页面级的TextBox输入一个随机数" onclick="Button1_Click" />
然后编写后台代码:
[csharp] //定义一个不含数据的事件处理方法
public event EventHandler Click;
protected void Button1_Click(object sender, EventArgs e)
{
Click(this, EventArgs.Empty);
}
//定义一个不含数据的事件处理方法
public event EventHandler Click;
protected void Button1_Click(object sender, EventArgs e)
{
Click(this, EventArgs.Empty);
}
好了,用户控件我们就算完成了,现在新建一个WebForm1.aspx页面,在页面上添加一个Textbox,并把用户控件添加到该页面:
[html] <%@ 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">
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</p>
</form>
</body>
</html>
<%@ 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">
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</p>
</form>
</body>
</html>
然后我们在后台对用户控件的事件添加一个函数响应,代码如下:
[csharp] protected void Page_Load(object sender, EventArgs e)
{
WebUserControl11.Click += new EventHandler(WebUserControl11_Click);
}
void WebUserControl11_Click(object sender, EventArgs e)
{
TextBox1.Text = new Random().Next(1000, 9999).ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl11.Click += new EventHandler(WebUserControl11_Click);
}
void WebUserControl11_Click(object sender, EventArgs e)
{
TextBox1.Text = new Random().Next(1000, 9999).ToString();
}
到这,我们的示例就完成了,我们在aspx可以捕捉到用户控件的事件,做我们想做的事情了。
二、带有事件数据的示例
当然,有时候我们不仅想要在aspx页面捕捉到事件,还希望能获取到一些参数,这时候EventArgs就需要出场了
首先,新建一个web用户控件WebUserControl2.ascx
在页面上添加两个按钮,分别响应Click事件,如下:
[html] <asp:Button ID="Button1" runat="server" Text="修改页面标题为编辑"
onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="修改页面标题为新增"
onclick="Button2_Click" />
<asp:Button ID="Button1" runat="server" Text="修改页面标题为编辑"
onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="修改页面标题为新增"
onclick="Button2_Click" />
C#代码,首先添加一个类ChangePageTitleEventArgs,用来传递事件数据,代码如下:
[csharp] public class ChangePageTitleEventArgs : EventArgs
{
public ChangePageTitleEventArgs(string title)
{
this.Title = title;
}
public string Title
{
get;
private set;
}
}
public class ChangePageTitleEventArgs : EventArgs
{
public ChangePageTitleEventArgs(string title)
{
this.Title = title;
}
public string Title
{
get;
private set;
}
}
然后我们来编写用户控件的后台代码:
[csharp] public event EventHandler<ChangePageTitleEventArgs> ChangePageTitle;
protected void Button1_Click(object sender, EventArgs e)
{
ChangePageTitle(this,new ChangePageTitleEventArgs("编辑"));
}
protected void Button2_Click(object sender, EventArgs e)
{
ChangePageTitle(this, new ChangePageTitleEventArgs("新增"));
}
public event EventHandler<ChangePageTitleEventArgs> ChangePageTitle;
protected void Button1_Click(object sender, EventArgs e)
{
ChangePageTitle(this,new ChangePageTitleEventArgs("编辑"));
}
protected void Button2_Click(object sender, EventArgs e)
{
ChangePageTitle(this, new ChangePageTitleEventArgs("新增"));
}
用户控件的代码就OK了,然后我们新建一个WebForm2.aspx页面,将用户控件添加到该页面
[csharp] <%@ Register src="WebUserControl2.ascx" tagname="WebUserControl2" 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">
<uc1:WebUserControl2 ID="WebUserControl21" runat="server" />
</form>
</body>
</html>
<%@ Register src="WebUserControl2.ascx" tagname="WebUserControl2" 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">
<uc1:WebUserControl2 ID="WebUserControl21" runat="server" />
</form>
</body>
</html>
编写后台代码,对用户控件的时间添加处理函数:
[csharp] protected void Page_Load(object sender, EventArgs e)
{
WebUserControl21.ChangePageTitle += new EventHandler<ChangePageTitleEventArgs>(WebUserControl21_ChangePageTitle);
}
void WebUserControl21_ChangePageTitle(object sender, ChangePageTitleEventArgs e)
{
this.Page.Title = e.Title;
}
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl21.ChangePageTitle += new EventHandler<ChangePageTitleEventArgs>(WebUserControl21_ChangePageTitle);
}
void WebUserControl21_ChangePageTitle(object sender, ChangePageTitleEventArgs e)
{
this.Page.Title = e.Title;
}
好了,很简单的我们就完成了代码,运行测试一下,效果如下:
摘自 涂剑凯的专栏