ASP.NET - 自定义控件处理页面事件(控件与页面数据交互)的方法

//用委托的方法实现
//控件代码
public delegate void DelegateFunction( string sPageTitle );
private DelegateFunction delegateChangePageTitle = null;
public DelegateFunction ChangePT
{
get { return delegateChangePageTitle; }set { delegateChangePageTitle = value; }
}
protected void BtEdit_Click( object sender, EventArgs e )
{
if ( delegateChangePageTitle != null ) delegateChangePageTitle( "Edit" );
}
protected void BtAdd_Click( object sender, EventArgs e )
{
if ( delegateChangePageTitle != null ) delegateChangePageTitle( "Add" );
} //页面 后台代码
protected void Page_Load( object sender, EventArgs e )
{ myWebControl11.ChangePT = ChangePT; }
void ChangePT( string sTmp )
{ this.Page.Title = sTmp; }
//页面 前代码
<%@ Register src="myWebControl.ascx" tagname="myWebControl1" tagprefix="uc1" %>
<uc1:myWebControl1 ID="myWebControl11" runat="server" /> //用事件的方法实现 控件定义事件 页面实现
//控件代码
//定义一个不含数据的事件处理方法
public event EventHandler ClickModifyTitle;
protected void btCreateValue_Click( object sender, EventArgs e )
{ ClickModifyTitle( this, EventArgs.Empty ); } //页面后台代码
protected void Page_Load( object sender, EventArgs e )
{
myWebControl11.ClickModifyTitle +=new EventHandler(myWebControl11_ClickModifyTitle);
} void myWebControl11_ClickModifyTitle( object sender, EventArgs e )
{
this.Page.Title += new Random().Next( , ).ToString();
}
上一篇:多线程 or 多进程 (转强力推荐)


下一篇:node.js处理http请求总结