服务器控件开发 绑定事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace ServerControl
{
    [DefaultProperty("Text")]
    [DefaultEvent("TextChanged")]
    [ToolboxData("<{0}:KingTextBoxCanPostevent runat=server></{0}:KingTextBoxCanPostevent>")]
    public class KingTextBoxCanPostevent : Control,IPostBackDataHandler,IRaiseItemChangedEvents
    {
        public KingTextBoxCanPostevent() { }

        public string Text
        {
            get 
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }
            set
            { 
                ViewState["Text"]=value;
            }
        }


        public bool AutoBostBack
        {
            get;
            set;
        }

        protected override void OnPreRender(EventArgs e)
        {
            PostBackOptions pbo = new PostBackOptions(this);
            pbo.AutoPostBack = AutoBostBack;
            pbo.PerformValidation = true;
            pbo.TrackFocus = true;
            pbo.ClientSubmit = true;
            pbo.RequiresJavaScriptProtocol = false;
            string strPostBackCode = this.Page.ClientScript.GetPostBackEventReference(pbo);

            StringBuilder strPostBackFromClient = new StringBuilder();
            strPostBackFromClient.Append(" function PostBackFromClient_"+this.ClientID+"()");
            strPostBackFromClient.Append("{");
            strPostBackFromClient.Append(strPostBackCode+";");
            strPostBackFromClient.Append("}");

            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "PostBackFromClient_" + this.ClientID))
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"PostBackFromClient_"+this.ClientID,strPostBackFromClient.ToString(),true);
            }

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //base.Render(writer);
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type=\"text\" name=");
            sb.Append("\""+this.UniqueID+"\"");
            sb.Append(" value=");
            sb.Append("\"" + HttpUtility.HtmlEncode(Text)+ "\"");
            sb.Append(" οnblur='" + "PostBackFromClient_" + this.ClientID + "();'");
            sb.Append(" />");
            writer.Write(sb.ToString());
        }

        public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            string strOldValue = Text;
            string strNewValue = postCollection[this.UniqueID];
            if (strOldValue == null || (strOldValue != null && !strOldValue.Equals(strNewValue)))
            {
                this.Text = strNewValue;
                return true;
            }
            return false;
        }



        public event EventHandler TextChanged;

        protected virtual void OnTextChanged(EventArgs e)
        {
            if (TextChanged != null)
            {
                TextChanged(this, e);
            }
        }

        #region IRaiseItemChangedEvents Members

        public bool RaisesItemChangedEvents
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IPostBackDataHandler Members


        public void RaisePostDataChangedEvent()
        {
            OnTextChanged(EventArgs.Empty);
        }

        #endregion
    }
}

-------------------------------------------------------------------------------------


复合控件的事件处理机制:

private static readonly object TextChangedKeyObject = new object();
        public event EventHandler TextChanged2
        {
            add
            {
                base.Events.AddHandler(TextChangedKeyObject, value);
            }
            remove
            {
                base.Events.RemoveHandler(TextChangedKeyObject, value);
            }
        }

        protected virtual void OnTextChanged2(EventArgs e)
        {
            EventHandler handler = base.Events[TextChangedKeyObject] as EventHandler;
            if (handler != null)
            {
                handler(this, e);
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值