学习笔记:ASP.NET TextBox 服务器控件源码

[ControlBuilder(typeof(TextBoxControlBuilder)), DefaultProperty("Text"), ParseChildren(false), DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ValidationProperty("Text"), DefaultEvent("TextChanged"), PermissionSet(SecurityAction.LinkDemand, XML="<PermissionSet class=\"System.Security.PermissionSet\"\r\n version=\"1\">\r\n <IPermission class=\"System.Web.AspNetHostingPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\n version=\"1\"\r\n Level=\"Minimal\"/>\r\n</PermissionSet>\r\n"), PermissionSet(SecurityAction.InheritanceDemand, XML="<PermissionSet class=\"System.Security.PermissionSet\"\r\n version=\"1\">\r\n <IPermission class=\"System.Web.AspNetHostingPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\n version=\"1\"\r\n Level=\"Minimal\"/>\r\n</PermissionSet>\r\n")]public class TextBox : WebControl, IPostBackDataHandler
 {
   // Fields
   private static readonly object EventTextChanged;

   // Events
   [WebCategory("Action"), WebSysDescription("TextBox_OnTextChanged")]
   public event EventHandler TextChanged;

   // Methods
   static TextBox();
   public TextBox();
   protected override void AddAttributesToRender(HtmlTextWriter writer);
   protected override void AddParsedSubObject(object obj);
   protected override void OnPreRender(EventArgs e);
   protected virtual void OnTextChanged(EventArgs e);
   protected override void Render(HtmlTextWriter writer);
   bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection);
   void IPostBackDataHandler.RaisePostDataChangedEvent();

   // Properties
   [WebCategory("Behavior"), WebSysDescription("TextBox_AutoPostBack"), DefaultValue(false)]
   public virtual bool AutoPostBack { get; set; }
   [DefaultValue(0), WebSysDescription("TextBox_Columns"), Bindable(true), WebCategory("Appearance")]
   public virtual int Columns { get; set; }
   [Bindable(true), WebCategory("Behavior"), DefaultValue(0), WebSysDescription("TextBox_MaxLength")]
   public virtual int MaxLength { get; set; }
   [WebCategory("Behavior"), Bindable(true), DefaultValue(false), WebSysDescription("TextBox_ReadOnly")]
   public virtual bool ReadOnly { get; set; }
   [Bindable(true), WebCategory("Behavior"), DefaultValue(0), WebSysDescription("TextBox_Rows")]
   public virtual int Rows { get; set; }
   private bool SaveTextViewState { get; }
   protected override HtmlTextWriterTag TagKey { get; }
   [Bindable(true), WebCategory("Appearance"), DefaultValue(""), WebSysDescription("TextBox_Text"), PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
   public virtual string Text { get; set; }
   [DefaultValue(0), WebSysDescription("TextBox_TextMode"), WebCategory("Behavior")]
   public virtual TextBoxMode TextMode { get; set; }
   [WebCategory("Layout"), DefaultValue(true), WebSysDescription("TextBox_Wrap")]
   public virtual bool Wrap { get; set; }
 }
 [ControlBuilder(typeof(TextBoxControlBuilder)), DefaultProperty("Text"), ParseChildren(false), DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ValidationProperty("Text"), DefaultEvent("TextChanged"), PermissionSet(SecurityAction.LinkDemand, XML="<PermissionSet class=\"System.Security.PermissionSet\"\r\n version=\"1\">\r\n <IPermission class=\"System.Web.AspNetHostingPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\n version=\"1\"\r\n Level=\"Minimal\"/>\r\n</PermissionSet>\r\n"), PermissionSet(SecurityAction.InheritanceDemand, XML="<PermissionSet class=\"System.Security.PermissionSet\"\r\n version=\"1\">\r\n <IPermission class=\"System.Web.AspNetHostingPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\n version=\"1\"\r\n Level=\"Minimal\"/>\r\n</PermissionSet>\r\n")]
 public class TextBox : WebControl, IPostBackDataHandler
 {
   // Fields
   private static readonly object EventTextChanged = new object();

   // Events
   [WebCategory("Action"), WebSysDescription("TextBox_OnTextChanged")]
   public event EventHandler TextChanged
   {
   add
   {
   base.Events.AddHandler(EventTextChanged, value);
   }
   remove
   {
   base.Events.RemoveHandler(EventTextChanged, value);
   }
   }

   // Methods
   public TextBox() : base(HtmlTextWriterTag.Input)
   {
   }

   protected override void AddAttributesToRender(HtmlTextWriter writer)
   {
   int maxLength;
   if (this.Page != null)
   {
   this.Page.VerifyRenderingInServerForm(this);
   }
   writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
   switch (this.TextMode)
   {
   case TextBoxMode.SingleLine:
   {
   writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
   string text = this.Text;
   if (text.Length > 0)
   {
   writer.AddAttribute(HtmlTextWriterAttribute.Value, text);
   }
   break;
   }
   case TextBoxMode.Password:
   writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
   break;

   case TextBoxMode.MultiLine:
   maxLength = this.Rows;
   if (maxLength > 0)
   {
   writer.AddAttribute(HtmlTextWriterAttribute.Rows, maxLength.ToString(NumberFormatInfo.InvariantInfo));
   }
   maxLength = this.Columns;
   if (maxLength > 0)
   {
   writer.AddAttribute(HtmlTextWriterAttribute.Cols, maxLength.ToString(NumberFormatInfo.InvariantInfo));
   }
   if (!this.Wrap)
   {
   writer.AddAttribute(HtmlTextWriterAttribute.Wrap, "off");
   }
   goto Label_00FF;
   }
   maxLength = this.MaxLength;
   if (maxLength > 0)
   {
   writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, maxLength.ToString(NumberFormatInfo.InvariantInfo));
   }
   maxLength = this.Columns;
   if (maxLength > 0)
   {
   writer.AddAttribute(HtmlTextWriterAttribute.Size, maxLength.ToString(NumberFormatInfo.InvariantInfo));
   }
   Label_00FF:
   if (this.ReadOnly)
   {
   writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");
   }
   if (this.AutoPostBack && (this.Page != null))
   {
   string postBackClientEvent = this.Page.GetPostBackClientEvent(this, "");
   if (base.HasAttributes)
   {
   string str3 = base.Attributes["onchange"];
   if (str3 != null)
   {
   postBackClientEvent = str3 + postBackClientEvent;
   base.Attributes.Remove("onchange");
   }
   }
   writer.AddAttribute(HtmlTextWriterAttribute.Onchange, postBackClientEvent);
   writer.AddAttribute("language", "javascript");
   }
   base.AddAttributesToRender(writer);
   }

   protected override void AddParsedSubObject(object obj)
   {
   if (!(obj is LiteralControl))
   {
   throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Have_Children_Of_Type", "TextBox", obj.GetType().Name.ToString()));
   }
   this.Text = ((LiteralControl) obj).Text;
   }

   protected override void OnPreRender(EventArgs e)
   {
   base.OnPreRender(e);
   if (!this.SaveTextViewState)
   {
   this.ViewState.SetItemDirty("Text", false);
   }
   if (((this.Page != null) && this.AutoPostBack) && this.Enabled)
   {
   this.Page.RegisterPostBackScript();
   }
   }

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

   protected override void Render(HtmlTextWriter writer)
   {
   this.RenderBeginTag(writer);
   if (this.TextMode == TextBoxMode.MultiLine)
   {
   HttpUtility.HtmlEncode(this.Text, writer);
   }
   this.RenderEndTag(writer);
   }

   bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
   {
   string text = this.Text;
   string str2 = postCollection[postDataKey];
   if (!text.Equals(str2))
   {
   this.Text = str2;
   return true;
   }
   return false;
   }

   void IPostBackDataHandler.RaisePostDataChangedEvent()
   {
   this.OnTextChanged(EventArgs.Empty);
   }

   // Properties
   [WebCategory("Behavior"), WebSysDescription("TextBox_AutoPostBack"), DefaultValue(false)]
   public virtual bool AutoPostBack
   {
   get
   {
   object obj2 = this.ViewState["AutoPostBack"];
   return ((obj2 != null) && ((bool) obj2));
   }
   set
   {
   this.ViewState["AutoPostBack"] = value;
   }
   }

   [DefaultValue(0), WebSysDescription("TextBox_Columns"), Bindable(true), WebCategory("Appearance")]
   public virtual int Columns
   {
   get
   {
   object obj2 = this.ViewState["Columns"];
   if (obj2 != null)
   {
   return (int) obj2;
   }
   return 0;
   }
   set
   {
   if (value < 0)
   {
   throw new ArgumentOutOfRangeException("value");
   }
   this.ViewState["Columns"] = value;
   }
   }

   [Bindable(true), WebCategory("Behavior"), DefaultValue(0), WebSysDescription("TextBox_MaxLength")]
   public virtual int MaxLength
   {
   get
   {
   object obj2 = this.ViewState["MaxLength"];
   if (obj2 != null)
   {
   return (int) obj2;
   }
   return 0;
   }
   set
   {
   if (value < 0)
   {
   throw new ArgumentOutOfRangeException("value");
   }
   this.ViewState["MaxLength"] = value;
   }
   }

   [WebCategory("Behavior"), Bindable(true), DefaultValue(false), WebSysDescription("TextBox_ReadOnly")]
   public virtual bool ReadOnly
   {
   get
   {
   object obj2 = this.ViewState["ReadOnly"];
   return ((obj2 != null) && ((bool) obj2));
   }
   set
   {
   this.ViewState["ReadOnly"] = value;
   }
   }

   [Bindable(true), WebCategory("Behavior"), DefaultValue(0), WebSysDescription("TextBox_Rows")]
   public virtual int Rows
   {
   get
   {
   object obj2 = this.ViewState["Rows"];
   if (obj2 != null)
   {
   return (int) obj2;
   }
   return 0;
   }
   set
   {
   if (value < 0)
   {
   throw new ArgumentOutOfRangeException("value");
   }
   this.ViewState["Rows"] = value;
   }
   }

   private bool SaveTextViewState
   {
   get
   {
   if (this.TextMode == TextBoxMode.Password)
   {
   return false;
   }
   if (((base.Events[EventTextChanged] == null) && this.Enabled) && (this.Visible && (base.GetType() == typeof(TextBox))))
   {
   return false;
   }
   return true;
   }
   }

   protected override HtmlTextWriterTag TagKey
   {
   get
   {
   if (this.TextMode == TextBoxMode.MultiLine)
   {
   return HtmlTextWriterTag.Textarea;
   }
   return HtmlTextWriterTag.Input;
   }
   }

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

   [DefaultValue(0), WebSysDescription("TextBox_TextMode"), WebCategory("Behavior")]
   public virtual TextBoxMode TextMode
   {
   get
   {
   object obj2 = this.ViewState["Mode"];
   if (obj2 != null)
   {
   return (TextBoxMode) obj2;
   }
   return TextBoxMode.SingleLine;
   }
   set
   {
   if ((value < TextBoxMode.SingleLine) || (value > TextBoxMode.Password))
   {
   throw new ArgumentOutOfRangeException("value");
   }
   this.ViewState["Mode"] = value;
   }
   }

   [WebCategory("Layout"), DefaultValue(true), WebSysDescription("TextBox_Wrap")]
   public virtual bool Wrap
   {
   get
   {
   object obj2 = this.ViewState["Wrap"];
   if (obj2 != null)
   {
   return (bool) obj2;
   }
   return true;
   }
   set
   {
   this.ViewState["Wrap"] = value;
   }
   }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值