一简单实例
先来个简单点的首先新建一个 web用户控件WebUserControlascx
在页面上添加一个按钮并响应Click事件
[csharp] <asp:Button ID=Button runat=server Text=在页面级的TextBox输入一个随机数 onclick=Button_Click />
<asp:Button ID=Button runat=server Text=在页面级的TextBox输入一个随机数 onclick=Button_Click />
然后编写后台代码
[csharp] //定义一个不含数据的事件处理方法
public event EventHandler Click;
protected void Button_Click(object sender EventArgs e)
{
Click(this EventArgsEmpty);
}
//定义一个不含数据的事件处理方法
public event EventHandler Click;
protected void Button_Click(object sender EventArgs e)
{
Click(this EventArgsEmpty);
}
好了用户控件我们就算完成了现在新建一个WebFormaspx页面在页面上添加一个Textbox并把用户控件添加到该页面
[html] <%@ Register src=WebUserControlascx tagname=WebUserControl tagprefix=uc %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= >
<head runat=server>
<title></title>
</head>
<body>
<form id=form runat=server>
<div>
<asp:TextBox ID=TextBox runat=server></asp:TextBox>
<br />
<uc:WebUserControl ID=WebUserControl runat=server />
</div>
</form>
</body>
</html>
<%@ Register src=WebUserControlascx tagname=WebUserControl tagprefix=uc %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= >
<head runat=server>
<title></title>
</head>
<body>
<form id=form runat=server>
<div>
<asp:TextBox ID=TextBox runat=server></asp:TextBox>
<br />
<uc:WebUserControl ID=WebUserControl runat=server />
</div>
</form>
</body>
</html>
然后我们在后台对用户控件的事件添加一个函数响应代码如下
[csharp] protected void Page_Load(object sender EventArgs e)
{
WebUserControlClick += new EventHandler(WebUserControl_Click);
}
void WebUserControl_Click(object sender EventArgs e)
{
TextBoxText = new Random()Next( )ToString();
}
protected void Page_Load(object sender EventArgs e)
{
WebUserControlClick += new EventHandler(WebUserControl_Click);
}
void WebUserControl_Click(object sender EventArgs e)
{
TextBoxText = new Random()Next( )ToString();
}
到这我们的示例就完成了我们在aspx可以捕捉到用户控件的事件做我们想做的事情了
二带有事件数据的示例
当然有时候我们不仅想要在aspx页面捕捉到事件还希望能获取到一些参数这时候EventArgs就需要出场了
首先新建一个web用户控件WebUserControlascx
在页面上添加两个按钮分别响应Click事件如下
[html] <asp:Button ID=Button runat=server Text=修改页面标题为编辑
onclick=Button_Click />
<asp:Button ID=Button runat=server Text=修改页面标题为新增
onclick=Button_Click />
<asp:Button ID=Button runat=server Text=修改页面标题为编辑
onclick=Button_Click />
<asp:Button ID=Button runat=server Text=修改页面标题为新增
onclick=Button_Click />
C#代码首先添加一个类ChangePageTitleEventArgs用来传递事件数据代码如下
[csharp] public class ChangePageTitleEventArgs : EventArgs
{
public ChangePageTitleEventArgs(string title)
{
thisTitle = title;
}
public string Title
{
get;
private set;
}
}
public class ChangePageTitleEventArgs : EventArgs
{
public ChangePageTitleEventArgs(string title)
{
thisTitle = title;
}
public string Title
{
get;
private set;
}
}
然后我们来编写用户控件的后台代码
[csharp] public event EventHandler<ChangePageTitleEventArgs> ChangePageTitle;
protected void Button_Click(object sender EventArgs e)
{
ChangePageTitle(thisnew ChangePageTitleEventArgs(编辑));
}
protected void Button_Click(object sender EventArgs e)
{
ChangePageTitle(this new ChangePageTitleEventArgs(新增));
}
public event EventHandler<ChangePageTitleEventArgs> ChangePageTitle;
protected void Button_Click(object sender EventArgs e)
{
ChangePageTitle(thisnew ChangePageTitleEventArgs(编辑));
}
protected void Button_Click(object sender EventArgs e)
{
ChangePageTitle(this new ChangePageTitleEventArgs(新增));
}
用户控件的代码就OK了然后我们新建一个WebFormaspx页面将用户控件添加到该页面
[csharp] <%@ Register src=WebUserControlascx tagname=WebUserControl tagprefix=uc %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= >
<head runat=server>
<title></title>
</head>
<body>
<form id=form runat=server>
<uc:WebUserControl ID=WebUserControl runat=server />
</form>
</body>
</html>
<%@ Register src=WebUserControlascx tagname=WebUserControl tagprefix=uc %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= >
<head runat=server>
<title></title>
</head>
<body>
<form id=form runat=server>
<uc:WebUserControl ID=WebUserControl runat=server />
</form>
</body>
</html>
编写后台代码对用户控件的时间添加处理函数
[csharp] protected void Page_Load(object sender EventArgs e)
{
WebUserControlChangePageTitle += new EventHandler<ChangePageTitleEventArgs>(WebUserControl_ChangePageTitle);
}
void WebUserControl_ChangePageTitle(object sender ChangePageTitleEventArgs e)
{
thisPageTitle = eTitle;
}
protected void Page_Load(object sender EventArgs e)
{
WebUserControlChangePageTitle += new EventHandler<ChangePageTitleEventArgs>(WebUserControl_ChangePageTitle);
}
void WebUserControl_ChangePageTitle(object sender ChangePageTitleEventArgs e)
{
thisPageTitle = eTitle;
}
好了很简单的我们就完成了代码运行测试一下效果如下