软件人生

Jack(子游)

我的目标:做世界一流的软件,成为优秀的项目管理者 主要专注行业: Cms(content manage system) OA CRM 在线营销系统 在线调查
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

开发自定义控件 ------------Textbox 控件(1)

Posted on 2009-08-21 14:58  子游  阅读(809)  评论(0)    收藏  举报

                                     开发自定义控件 __Textbox控件(1)
  支持postback
   public cass SugaroaInput : WebControl
  {
     

        private static readonly object EventTextChanged = new object();

        //支持事件
        public event EventHandler TextChanged
        {
            add
            {
                base.Events.AddHandler(EventTextChanged, value);
            }
            remove
            {
                base.Events.RemoveHandler(EventTextChanged, value);
            }
        }

       //post back
        protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {

            //Page.ClientScript.ValidateEvent(postDataKey, string.Empty);  
            string text = this.Text;
            string str2 = postCollection[postDataKey];
            if (!this.ReadOnly && !text.Equals(str2, StringComparison.Ordinal))
            {
                this.Text = str2;
                return true;
            }
            return false;
        }
    
      //输出html  

      protected override void Render(HtmlTextWriter writer)
        {
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);
             writer.Write("<input ");
            writer.WriteAttribute("type", "input");
            writer.WriteAttribute("id", this.ClientID );
            writer.WriteAttribute("name", this.ClientID );
           writer.WriteAttribute("value", this.Text, true);
          writer.Write("/> ");

            base.Render(writer);
        }


 

       //Text change event handler
        protected virtual void OnTextChanged(EventArgs e)
        {
            EventHandler handler = (EventHandler)base.Events[EventTextChanged];
            if (handler != null)
            {
                handler(this, e);
            }
        }

              [Localizable(true), Bindable(true, BindingDirection.TwoWay), PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty), DefaultValue("")]
        public virtual string Text
        {
            get
            {
                string str = (string)this.ViewState["Text"];
                if (str != null)
                {
                    return str;
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["Text"] = value;
            }
        }


   }
快乐软件人生