My WebControl

前几天写Web Control,一阵不假思索,只图结果的狂写,总算是写了2个控件,再看了点资料,总结一下:

    1.关于IPostBackDataHandler接口

       实现两个方法即可:

    public virtual void RaisePostDataChangedEvent() {
         OnTextChanged(EventArgs.Empty)      和

    public virtual bool LoadPostData(string postDataKey,
         NameValueCollection postCollection)

       这个LoadPostData可把我搞惨了,PostDataCollection中的Item个数到底是依据什么的啊?看代码,找资料,搞了半天,还是不明白。冷静下来,仔细思考,其实他只可能是两种类型的集合,一是控件中所有用到的Viewstate键值对的集合,二是只是在客户端发生改变的。于是,懒人的最后一个办法:自己动手试试.....

      看看MSDN的例子:

using System;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;


namespace CustomWebFormsControls {

   [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
   public class MyTextBox: Control, IPostBackDataHandler {
   

      public String Text {
         get {
            return (String) ViewState["Text"];
         }

         set {
            ViewState["Text"] = value;
         }
      }     
     

      public event EventHandler TextChanged;
     

      public virtual bool LoadPostData(string postDataKey,
         NameValueCollection postCollection) {

         String presentValue = Text;
         String postedValue = postCollection[postDataKey];

         if (presentValue == null || !presentValue.Equals(postedValue)) {
            Text = postedValue;
            return true;
         }

         return false;
      }

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

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

      protected override void Render(HtmlTextWriter output) {
         output.Write("<INPUT type= text name = "+this.UniqueID
            + " value = " + this.Text + " >");
      }
   }  
}
于是我加入了一个Viewstate值,但是发现postCollection中还是只有text值,于是猜测会是第二种可能。

动手写了一个webcontrol,输出两个Dhtml控件,一个<input type="text"/>和<input type=checkbox>然后.......

偶在postCollection终于得到了2个值......

2.IEnumerable

 作用: 可以支持用户使用foreach循环

 实现此接口,必须同时实现相对应的IEnumerator : Class NSEnumerator : IEnumerator

3.ICloneable

对象的深拷贝

实现public object Clone() { }

4.IDisposable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值