请教自定义控件问题,IPostDataHandler接口和IPostDataEventHandler接口。
下面是msdn上面的自定义控件源代码,在aspx页面引用它后,运行aspx页面,该句if (!presentValue.Equals(postedValue))报错,错误信息:System.NullReferenceException: 未将对象引用设置到对象的实例。
---------------------------------下面是代码---------------------------------
using System;
using System.Web;
using System.Web.UI;
using System.Collections.Specialized;
namespace CustomControls{
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 values) {
String presentValue = Text;
String postedValue = values[postDataKey];
if (!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 + " >");
}
}
}