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( 1000, 9999 ).ToString();
        }

 

posted on 2014-08-16 12:22  扶强  阅读(641)  评论(0编辑  收藏  举报