IPostBackDataHandler和IPostBackEventHandler

IPostBackDataHandler

定义:Asp.Net 服务器控件为自动加载回发数据而必须实现的方法。

例如:

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

namespace Controls.Pager
{
    [ToolboxData("<{0}:PostDataBackComponent runat='server'></{0}:PostDataBackComponent>")]
    public class PostDataBackComponent : Control, IPostBackDataHandler
    {
        public PostDataBackComponent()
        { }

        public string Text
        {
            get
            {
                object text = ViewState["Text"];
                if (text == null)
                    return string.Empty;
                else
                    return (string)text;
            }
            set
            {
                ViewState["Text"] = value;
            }
        }

        public event EventHandler TextChanged;

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

        /// <summary>
        /// 主要作用是可以获取回发回来的数据.
        /// </summary>
        /// <param name="postDataKey">标识控件的关键字</param>
        /// <param name="postCollection">发送数据的集合</param>
        /// <returns>return true则,将调用.RaisePostDataChangedEvent产生回发数据发生变化的事件</returns>
        public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            string postedData = postCollection[postDataKey];//回发回来的字符串

            if (!this.Text.Equals(postedData))
            {
                this.Text = postedData;
                return true;
            }
            else
            {
                return false;
            }
        }

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

        protected override void Render(HtmlTextWriter writer)
        {
            //base.Render(writer);
            writer.Write("<input type='text' name='" + this.UniqueID + "' value='" + this.Text + "'/>");
        }
    }
}

在页面上有submit表单时,text文本框的内容不会变化,不会被初始化。

IPostBackEventHandler 接口

定义: ASP.NET 服务器控件为处理回发事件而必须实现的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Controls.Pager
{
     [ToolboxData("<{0}:PostEventBackComponent runat='server'></{0}:PostDataBackComponent>")]
   public class PostEventBackComponent:Control,IPostBackEventHandler
    {
         /// <summary>
         /// 文本
         /// </summary>
         public string Text
         {
             get
             {
                 object text = ViewState["Text"];
                 if (text == null)
                     return string.Empty;
                 else
                     return (string)text;
             }
             set
             {
                 ViewState["Text"] = value;
             }
         }

         public event EventHandler Click;

         protected void OnClick(EventArgs e)
         {
             if (Click != null)
                 Click(this, e);
         }

         public void RaisePostBackEvent(string eventArgument)
         {
             OnClick(new EventArgs());
         }

         protected override void Render(HtmlTextWriter writer)
         {
             writer.Write("<INPUT TYPE = submit name = " + this.UniqueID + " Value = '"+Text+"' />");    
         }


    }
}

回发引发Click事件

 

posted @ 2014-08-26 11:48  聆听的风声  阅读(321)  评论(0)    收藏  举报