熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

Asp.net 控件開發—數據回傳

2022-06-13   來源: .NET編程 

  准備技術

  常規開發技術

  了解控件開發的呈現屬性狀態簡單原理

  內容

  數據回傳就是說客戶端將數據回交給服務器然後模型將會將處理後的數據回傳到表單中去如果開發的控件需要回傳的話就必須要去繼承於IPostBackDataHandler接口 繼承IPostBackDataHandler之後我們一般需要去重寫兩個方法

  LoadPostData()方法返回一個bool值表示該控件的值是否發生改變(需要我們去判斷)

  RaisPostDataChangeEvent()此方法是在LoadPostData方法返回true的時候去執行的

  一LoadPostData方法

  此方法的原型LoadPostData(string postDataKeySystemCollectionsSpecializedNameValueCollection postCollection)

  參數postDataKey就是回傳控件的UniqueID在客戶端表現為name屬性postCollection就是回傳的數據值的結合所以我們可以通過postCollection[postDataKey]來獲取本控制回傳的值所以我們就可以來比較原來控件的值跟回傳的值從而來確定LoadPostData方法返回true還是false

  要注意的一點是我們回傳時需要name屬性所以如果我們定義的控件要支持數據回傳的話就必須要去添加屬性name

  二RaisePostDataChangeEvent()方法

  RaisePostDataChangeEvent方法一般是在LoadPostData返回True的是否才去執行一般我們在此方法中去觸發一些事件TextChanged

  三實例

  下面就來模仿寫一個TextBox控件當然現在只是去實現數據回傳功能

  首先我們實現控件的一些屬性現在我們就讓其有Text屬性           [PersistenceMode(PersistenceModeEncodedInnerDefaultProperty)
                DefaultValue()
                Category(Behavior)
                Description(文本內容)]
                public string Text
                {
                    get
                    {
                        return thisViewState[Text] == null ? StringEmpty : (string)thisViewState[Text];
                    }
                    set
                    {
                        thisViewState[Text] = value;
                    }
                }

  然後我們把控件呈現控件是繼承於WebControl的            protected override HtmlTextWriterTag TagKey
                {
                    get
                    {
                        return HtmlTextWriterTagInput;
                    }
                }
                protected override void AddAttributesToRender(HtmlTextWriter writer)
                {
                    baseAddAttributesToRender(writer);
                    if (Page != null)
                    {
                        PageVerifyRenderingInServerForm(this);
                    }
                    writerAddAttribute(HtmlTextWriterAttributeType text);
                    writerAddAttribute(HtmlTextWriterAttributeName thisUniqueID);
                    if (!Enabled)
                    {
                        writerAddAttribute(HtmlTextWriterAttributeDisabled disabled);
                    }
                    writerAddAttribute(HtmlTextWriterAttributeValue thisText);
                }
                protected override void AddParsedSubObject(object obj)
                {
                    if (!(obj is Literal))
                        return;
                    baseAddParsedSubObject(obj);
                }

  這裡要注意的是一定要注冊其前台name屬性最後我們看如何讓控件支持數據回傳             public bool LoadPostData(string postKey SystemCollectionsSpecializedNameValueCollection valueCollection)
                {
                    string postData = valueCollection[postKey]ToString();
                    string strText = thisText;
                    if (strTextEquals(postData StringComparisonOrdinal))
                        return false;
                    thisText = postData;
                    return true;
                }
                public void RaisePostDataChangedEvent()
                {
                    EventHandler handler = (EventHandler)Events[_objEvent];
                    if (handler != null)
                    {
                        handler(this EventArgsEmpty);
                    }
                }

  其實原理很簡單就是判斷回傳過來的數據是否跟原來相等不相等返回true則執行RaisePostDataChangeEvent方法控件的全部代碼如下

  

  using System;
        using SystemCollectionsGeneric;
        using SystemComponentModel;
        using SystemText;
        using SystemWeb;
        using SystemWebUI;
        using SystemWebUIWebControls;
        namespace HenllyeeControls
        {
            [DefaultProperty(Text)
            ParseChildren(trueText)
            DefaultEvent(TextChanged)
            ControlBuilder(typeof(HenllyeeTextBuider))]
            public class HenllyeeText:WebControlIPostBackDataHandler
            {
                Properties#region Properties
                [PersistenceMode(PersistenceModeEncodedInnerDefaultProperty)
                DefaultValue()
                Category(Behavior)
                Description(文本內容)]
                public string Text
                {
                    get
                    {
                        return thisViewState[Text] == null ? StringEmpty : (string)thisViewState[Text];
                    }
                    set
                    {
                        thisViewState[Text] = value;
                    }
                }
                #endregion
                Events#region Events
                private static object _objEvent;
                [Category(Action)]
                public event EventHandler TextChanged
                {
                    add
                    {
                        thisEventsAddHandler(_objEvent value);
                    }
                    remove
                    {
                        thisEventsRemoveHandler(_objEvent value);
                    }
                }
                #endregion
                Render#region Render
                protected override HtmlTextWriterTag TagKey
                {
                    get
                    {
                        return HtmlTextWriterTagInput;
                    }
                }
                protected override void AddAttributesToRender(HtmlTextWriter writer)
                {
                    baseAddAttributesToRender(writer);

  if (Page != null)
                    {
                        PageVerifyRenderingInServerForm(this);
                    }
                    writerAddAttribute(HtmlTextWriterAttributeType text);
                    writerAddAttribute(HtmlTextWriterAttributeName thisUniqueID);
                    if (!Enabled)
                    {
                        writerAddAttribute(HtmlTextWriterAttributeDisabled disabled);
                    }
                    writerAddAttribute(HtmlTextWriterAttributeValue thisText);
                }
                protected override void AddParsedSubObject(object obj)
                {
                    if (!(obj is Literal))
                        return;
                    baseAddParsedSubObject(obj);
                }
                #endregion
                IPostBackData#region IPostBackData
                public bool LoadPostData(string postKey SystemCollectionsSpecializedNameValueCollection valueCollection)
                {
                    string postData = valueCollection[postKey]ToString();
                    string strText = thisText;
                    if (strTextEquals(postData StringComparisonOrdinal))
                        return false;
                    thisText = postData;
                    return true;
                }
                public void RaisePostDataChangedEvent()
                {
                    EventHandler handler = (EventHandler)Events[_objEvent];
                    if (handler != null)
                    {
                        handler(this EventArgsEmpty);
                    }
                }
                #endregion
            }
            public class HenllyeeTextBuider : ControlBuilder
            {
                public override bool AllowWhitespaceLiterals()
                {
                    return false;
                }
                public override bool HtmlDecodeLiterals()
                {
                    return true;
                }
            }
        }

  我們新建一個頁面中添加剛才的控件跟一個Button可是發現能支持數據回傳了


From:http://tw.wingwit.com/Article/program/net/201311/11451.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.