前几天写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