zoukankan      html  css  js  c++  java
  • 控件生命周期

    第一次加载主要事件如下:
    1:TrackViewState
    2:OnPreRender
    3:SaveViewState()
    4:Render

    回传时页面加载主要事件如下:

    page.isPostBack
    1:TrackViewState
    2:LoadViewState
    3:LoadPostData
    4:RaisePostDataChangedEvent
    5RaisePostBackEvent
    6:OnPreRender
    7:SaveViewState
    8:Render
    如果要回传。在Render的PostBackOptions的AutoPostBack = true;
    如果要加载数据。在OnPreRender加this.Page.RegisterRequiresPostBack(this);

    事例:

      1using System;
      2using System.Collections.Specialized;
      3using System.ComponentModel;
      4using System.Globalization;
      5using System.Security.Permissions;
      6using System.Web;
      7using System.Web.UI;
      8using System.Web.UI.WebControls;
      9
     10[assembly: TagPrefix("Invent.Controls""Invent")]
     11namespace InventControls
     12{
     13   // [SupportsEventValidation, ControlValueProperty("Checked"), DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultEvent("CheckedChanged"), Designer("System.Web.UI.Design.WebControls.CheckBoxDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultProperty("Text"), AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
     14    public class InventCheckBox : WebControl, IPostBackDataHandler, ICheckBoxControl
     15    {
     16        [Description("Control_OnServerCheckChanged"), Category("Action")]
     17        public event EventHandler CheckedChanged
     18        {
     19            add
     20            {
     21                base.Events.AddHandler(InventCheckBox.EventCheckedChanged, value);
     22            }

     23            remove
     24            {
     25                base.Events.RemoveHandler(InventCheckBox.EventCheckedChanged, value);
     26            }

     27        }

     28
     29        static InventCheckBox()
     30        {
     31            InventCheckBox.EventCheckedChanged = new object();
     32        }

     33
     34        public InventCheckBox() : base(HtmlTextWriterTag.Input)
     35        {
     36        }

     37
     38        protected override void AddAttributesToRender(HtmlTextWriter writer)
     39        {
     40            //base.AddDisplayInlineBlockIfNeeded(writer);
     41        }

     42
     43        protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
     44        {
     45            bool flag1 = false;
     46            string text1 = postCollection[postDataKey];
     47            bool flag2 = !string.IsNullOrEmpty(text1);
     48            if (flag2)
     49            {
     50                //base.ValidateEvent(postDataKey);
     51            }

     52            flag1 = flag2 != this.Checked;
     53            this.Checked = flag2;
     54            return flag1;
     55        }

     56
     57        protected override void LoadViewState(object savedState)
     58        {
     59            if (savedState != null)
     60            {
     61                Triplet triplet1 = (Triplet) savedState;
     62                base.LoadViewState(triplet1.First);
     63                if (triplet1.Second != null)
     64                {
     65                    if (this._inputAttributesState == null)
     66                    {
     67                        this._inputAttributesState = new StateBag();
     68                        ((IStateManager)this._inputAttributesState).TrackViewState();
     69                    }

     70                    ((IStateManager)this._inputAttributesState).LoadViewState(triplet1.Second);
     71                }

     72                if (triplet1.Third != null)
     73                {
     74                    if (this._labelAttributesState == null)
     75                    {
     76                        this._labelAttributesState = new StateBag();
     77                        ((IStateManager)this._labelAttributesState).TrackViewState();
     78                    }

     79                    ((IStateManager)this._labelAttributesState).LoadViewState(triplet1.Second);
     80                }

     81            }

     82        }

     83
     84        protected virtual void OnCheckedChanged(EventArgs e)
     85        {
     86            EventHandler handler1 = (EventHandler) base.Events[InventCheckBox.EventCheckedChanged];
     87            if (handler1 != null)
     88            {
     89                handler1(this, e);
     90            }

     91        }

     92
     93        protected override void OnPreRender(EventArgs e)
     94        {
     95            base.OnPreRender(e);
     96            bool flag1 = this.AutoPostBack;
     97            if ((this.Page != null&& base.IsEnabled)
     98            {
     99                this.Page.RegisterRequiresPostBack(this);
    100                //if (flag1)
    101                //{
    102                //    this.Page.RegisterPostBackScript();
    103                //    this.Page.RegisterFocusScript();
    104                //    if (this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0))
    105                //    {
    106                //        this.Page.RegisterWebFormsScript();
    107                //    }
    108                //}
    109            }

    110            if (!this.SaveCheckedViewState(flag1))
    111            {
    112                this.ViewState.SetItemDirty("Checked"false);
    113                //if ((this.Page != null) && base.IsEnabled)
    114                //{
    115                //    this.Page.RegisterEnabledControl(this);
    116                //}
    117            }

    118        }

    119
    120        protected virtual void RaisePostDataChangedEvent()
    121        {
    122            if (this.AutoPostBack)
    123            {
    124                //this.Page.AutoPostBackControl = this;
    125                if (this.CausesValidation)
    126                {
    127                    this.Page.Validate(this.ValidationGroup);
    128                }

    129            }

    130            this.OnCheckedChanged(EventArgs.Empty);
    131        }

    132
    133        protected override void Render(HtmlTextWriter writer)
    134        {
    135            this.AddAttributesToRender(writer);
    136            if (this.Page != null)
    137            {
    138                this.Page.VerifyRenderingInServerForm(this);
    139            }

    140            bool flag1 = false;
    141            if (base.ControlStyleCreated)
    142            {
    143                Style style1 = base.ControlStyle;
    144                if (!style1.IsEmpty)
    145                {
    146                    style1.AddAttributesToRender(writer, this);
    147                    flag1 = true;
    148                }

    149            }

    150            if (!base.IsEnabled)
    151            {
    152                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
    153                flag1 = true;
    154            }

    155            string text1 = this.ToolTip;
    156            if (text1.Length > 0)
    157            {
    158                writer.AddAttribute(HtmlTextWriterAttribute.Title, text1);
    159                flag1 = true;
    160            }

    161            string text2 = null;
    162            if (base.HasAttributes)
    163            {
    164                System.Web.UI.AttributeCollection collection1 = base.Attributes;
    165                string text3 = collection1["value"];
    166                if (text3 != null)
    167                {
    168                    collection1.Remove("value");
    169                }

    170                text2 = collection1["onclick"];
    171                if (text2 != null)
    172                {
    173                    text2 = Util.EnsureEndWithSemiColon(text2);
    174                    collection1.Remove("onclick");
    175                }

    176                if (collection1.Count != 0)
    177                {
    178                    collection1.AddAttributes(writer);
    179                    flag1 = true;
    180                }

    181                if (text3 != null)
    182                {
    183                    collection1["value"= text3;
    184                }

    185            }

    186            if (flag1)
    187            {
    188                writer.RenderBeginTag(HtmlTextWriterTag.Span);
    189            }

    190            string text4 = this.Text;
    191            string text5 = this.ClientID;
    192            if (text4.Length != 0)
    193            {
    194                if (this.TextAlign == System.Web.UI.WebControls.TextAlign.Left)
    195                {
    196                    this.RenderLabel(writer, text4, text5);
    197                    this.RenderInputTag(writer, text5, text2);
    198                }

    199                else
    200                {
    201                    this.RenderInputTag(writer, text5, text2);
    202                    this.RenderLabel(writer, text4, text5);
    203                }

    204            }

    205            else
    206            {
    207                this.RenderInputTag(writer, text5, text2);
    208            }

    209            if (flag1)
    210            {
    211                writer.RenderEndTag();
    212            }

    213        }

    214
    215        internal virtual void RenderInputTag(HtmlTextWriter writer, string clientID, string onClick)
    216        {
    217            if (clientID != null)
    218            {
    219                writer.AddAttribute(HtmlTextWriterAttribute.Id, clientID);
    220            }

    221            writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
    222            if (this.UniqueID != null)
    223            {
    224                writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
    225            }

    226            if (this._valueAttribute != null)
    227            {
    228                writer.AddAttribute(HtmlTextWriterAttribute.Value, this._valueAttribute);
    229            }

    230            if (this.Checked)
    231            {
    232                writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
    233            }

    234            if (!base.IsEnabled)
    235            {
    236                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
    237            }

    238            if ((this.AutoPostBack && (this.Page != null)))
    239            {
    240                PostBackOptions options1 = new PostBackOptions(thisstring.Empty);
    241                if (this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0))
    242                {
    243                    options1.PerformValidation = true;
    244                    options1.ValidationGroup = this.ValidationGroup;
    245                }

    246                if (this.Page.Form != null)
    247                {
    248                    options1.AutoPostBack = true;
    249                }

    250                onClick = Util.MergeScript(onClick, this.Page.ClientScript.GetPostBackEventReference(options1, true));
    251                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
    252               
    253            }

    254            else
    255            {
    256                if (this.Page != null)
    257                {
    258                    this.Page.ClientScript.RegisterForEventValidation(this.UniqueID);
    259                }

    260                if (onClick != null)
    261                {
    262                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
    263                }

    264            }

    265            string text1 = this.AccessKey;
    266            if (text1.Length > 0)
    267            {
    268                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, text1);
    269            }

    270            int num1 = this.TabIndex;
    271            if (num1 != 0)
    272            {
    273                writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, num1.ToString(NumberFormatInfo.InvariantInfo));
    274            }

    275            if ((this._inputAttributes != null&& (this._inputAttributes.Count != 0))
    276            {
    277                this._inputAttributes.AddAttributes(writer);
    278            }

    279            writer.RenderBeginTag(HtmlTextWriterTag.Input);
    280            writer.RenderEndTag();
    281        }

    282
    283        private void RenderLabel(HtmlTextWriter writer, string text, string clientID)
    284        {
    285            writer.AddAttribute(HtmlTextWriterAttribute.For, clientID);
    286            if ((this._labelAttributes != null&& (this._labelAttributes.Count != 0))
    287            {
    288                this._labelAttributes.AddAttributes(writer);
    289            }

    290            writer.RenderBeginTag(HtmlTextWriterTag.Label);
    291            writer.Write(text);
    292            writer.RenderEndTag();
    293        }

    294
    295        private bool SaveCheckedViewState(bool autoPostBack)
    296        {
    297            if ((((base.Events[InventCheckBox.EventCheckedChanged] != null|| !base.IsEnabled) || !this.Visible) || ((autoPostBack && (this.Page != null))))
    298            {
    299                return true;
    300            }

    301            Type type1 = base.GetType();
    302            if ((type1 != typeof(InventCheckBox)) && (type1 != typeof(RadioButton)))
    303            {
    304                return true;
    305            }

    306            return false;
    307        }

    308
    309        protected override object SaveViewState()
    310        {
    311            object obj1 = base.SaveViewState();
    312            object obj2 = null;
    313            object obj3 = null;
    314            object obj4 = null;
    315            if (this._inputAttributesState != null)
    316            {
    317                obj2 = ((IStateManager)this._inputAttributesState).SaveViewState();
    318            }

    319            if (this._labelAttributesState != null)
    320            {
    321                obj3 = ((IStateManager)this._labelAttributesState).SaveViewState();
    322            }

    323            if (((obj1 == null&& (obj2 == null)) && (obj3 == null))
    324            {
    325                return obj4;
    326            }

    327            return new Triplet(obj1, obj2, obj3);
    328        }

    329
    330        bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
    331        {
    332            return this.LoadPostData(postDataKey, postCollection);
    333        }

    334
    335        void IPostBackDataHandler.RaisePostDataChangedEvent()
    336        {
    337            this.RaisePostDataChangedEvent();
    338        }

    339
    340        protected override void TrackViewState()
    341        {
    342            base.TrackViewState();
    343            if (this._inputAttributesState != null)
    344            {
    345               ((IStateManager) this._inputAttributesState).TrackViewState();
    346            }

    347            if (this._labelAttributesState != null)
    348            {
    349                ((IStateManager)this._labelAttributesState).TrackViewState();
    350            }

    351        }

    352
    353
    354        [DefaultValue(false), Category("Behavior"), Description("CheckBox_AutoPostBack"), Themeable(false)]
    355        public virtual bool AutoPostBack
    356        {
    357            get
    358            {
    359                object obj1 = this.ViewState["AutoPostBack"];
    360                if (obj1 != null)
    361                {
    362                    return (bool) obj1;
    363                }

    364                return false;
    365            }

    366            set
    367            {
    368                this.ViewState["AutoPostBack"= value;
    369            }

    370        }

    371
    372        [DefaultValue(false), Themeable(false), Category("Behavior"), Description("AutoPostBackControl_CausesValidation")]
    373        public virtual bool CausesValidation
    374        {
    375            get
    376            {
    377                object obj1 = this.ViewState["CausesValidation"];
    378                if (obj1 != null)
    379                {
    380                    return (bool) obj1;
    381                }

    382                return false;
    383            }

    384            set
    385            {
    386                this.ViewState["CausesValidation"= value;
    387            }

    388        }

    389
    390        [Description("CheckBox_Checked"), Bindable(true, BindingDirection.TwoWay), DefaultValue(false), Themeable(false)]
    391        public virtual bool Checked
    392        {
    393            get
    394            {
    395                object obj1 = this.ViewState["Checked"];
    396                if (obj1 != null)
    397                {
    398                    return (bool) obj1;
    399                }

    400                return false;
    401            }

    402            set
    403            {
    404                this.ViewState["Checked"= value;
    405            }

    406        }

    407
    408        [Browsable(false), Description("CheckBox_InputAttributes"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    409        public System.Web.UI.AttributeCollection InputAttributes
    410        {
    411            get
    412            {
    413                if (this._inputAttributes == null)
    414                {
    415                    if (this._inputAttributesState == null)
    416                    {
    417                        this._inputAttributesState = new StateBag(true);
    418                        if (base.IsTrackingViewState)
    419                        {
    420                            ((IStateManager)this._inputAttributesState).TrackViewState();
    421                        }

    422                    }

    423                    this._inputAttributes = new System.Web.UI.AttributeCollection(this._inputAttributesState);
    424                }

    425                return this._inputAttributes;
    426            }

    427        }

    428
    429        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("CheckBox_LabelAttributes"), Browsable(false)]
    430        public System.Web.UI.AttributeCollection LabelAttributes
    431        {
    432            get
    433            {
    434                if (this._labelAttributes == null)
    435                {
    436                    if (this._labelAttributesState == null)
    437                    {
    438                        this._labelAttributesState = new StateBag(true);
    439                        if (base.IsTrackingViewState)
    440                        {
    441                            ((IStateManager)this._labelAttributesState).TrackViewState();
    442                        }

    443                    }

    444                    this._labelAttributes = new System.Web.UI.AttributeCollection(this._labelAttributesState);
    445                }

    446                return this._labelAttributes;
    447            }

    448        }

    449
    450        //protected override bool RequiresLegacyRendering
    451        //{
    452        //    get
    453        //    {
    454        //        return true;
    455        //    }
    456        //}
    457
    458        [Bindable(true), Localizable(true), Description("CheckBox_Text"), Category("Appearance"), DefaultValue("")]
    459        public virtual string Text
    460        {
    461            get
    462            {
    463                string text1 = (stringthis.ViewState["Text"];
    464                if (text1 != null)
    465                {
    466                    return text1;
    467                }

    468                return string.Empty;
    469            }

    470            set
    471            {
    472                this.ViewState["Text"= value;
    473            }

    474        }

    475
    476        [DefaultValue(2), Category("Appearance"), Description("WebControl_TextAlign")]
    477        public virtual System.Web.UI.WebControls.TextAlign TextAlign
    478        {
    479            get
    480            {
    481                object obj1 = this.ViewState["TextAlign"];
    482                if (obj1 != null)
    483                {
    484                    return (System.Web.UI.WebControls.TextAlign) obj1;
    485                }

    486                return System.Web.UI.WebControls.TextAlign.Right;
    487            }

    488            set
    489            {
    490                if ((value < System.Web.UI.WebControls.TextAlign.Left) || (value > System.Web.UI.WebControls.TextAlign.Right))
    491                {
    492                    throw new ArgumentOutOfRangeException("value");
    493                }

    494                this.ViewState["TextAlign"= value;
    495            }

    496        }

    497
    498        [DefaultValue(""), Themeable(false), Category("Behavior"), Description("PostBackControl_ValidationGroup")]
    499        public virtual string ValidationGroup
    500        {
    501            get
    502            {
    503                string text1 = (stringthis.ViewState["ValidationGroup"];
    504                if (text1 != null)
    505                {
    506                    return text1;
    507                }

    508                return string.Empty;
    509            }

    510            set
    511            {
    512                this.ViewState["ValidationGroup"= value;
    513            }

    514        }

    515
    516
    517        internal System.Web.UI.AttributeCollection _inputAttributes;
    518        private StateBag _inputAttributesState;
    519        private System.Web.UI.AttributeCollection _labelAttributes;
    520        private StateBag _labelAttributesState;
    521        private string _valueAttribute;
    522        private static readonly object EventCheckedChanged;
    523    }

    524}

    525
    526
     
  • 相关阅读:
    AutoFac
    MEF 基础简介 四
    MEF 基础简介 三
    MEF 基础简介 二
    MEF 基础简介 一
    Ioc原理理解
    .NET里面 abstract class和Interface有什么区别以及用法的展现?
    .NET-ORM框架EF-Code First代码优先
    SQL SERVER PIVOT与用法解释
    SQL Server 中的 NOLOCK 到底是什么意思?
  • 原文地址:https://www.cnblogs.com/KUDO/p/445500.html
Copyright © 2011-2022 走看看