return false;
11 //throw new NotImplementedException();
12 }
13
14 public void RaisePostDataChangedEvent()
15 {
16 //throw new NotImplementedException();
17 OnTextChanged(EventArgs.Empty);
18 }
并在测试项目的Default.aspx中的PostDataControl1控件上双击,在系统自动 生成的OnTextChanged事件函数内打上断点,看看如何处理。
(9).同样重新生成后将新的控件替换就的控件后运行程序。发现当文本框中的 数据改变后移出焦点,会进入PostDataControl1_OnTextChanged()事件函数中, 开发人员可以在该函数中处理相关逻辑了。
如上,将数据回传讲解的很细致了,当然,该控件还之能在焦点移出后触发 OnTextChanged事件,还不能像微软的TextBox控件那样文本改变后立即触发 OnTextChanged事件,有兴趣的不妨尝试下,有挑战才有乐趣嘛...
控件的完整代码还是附上:
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace PostBackEventControlDome
{
[DefaultProperty("Text")]
[DefaultEvent("TextChanged")]
[ToolboxData("<{0}:PostDataControl runat=server></ {0}:PostDataControl>")]
public class PostDataControl : Control,IPostBackDataHandler
{
[Bindable(true)]
[Category ("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState ["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void Render(HtmlTextWriter output)
{
String PostBackJs = Page.ClientScript.GetPostBackEventReference(this, "");
StringBuilder outString = new StringBuilder ();
outString.Append("<Input type=''text'' name=''");
outString.Append(this.UniqueID);
outString.Append("'' value=\"");
outString.Append(HttpUtility.HtmlEncode (Text));
outString.Append("\" onblur=\"");
outString.Append (PostBackJs);
outString.Append(";\" ></Input>");
output.Write(outString.ToString());
}
public event EventHandler TextChanged;
protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged != null)
{
TextChanged(this, e);
}
}
#region IPostBackDataHandler 成员
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string strOldValue = Text;
string strNewValue = postCollection[this.UniqueID];
if (strOldValue != null && (strNewValue != null && ! strOldValue.Equals(strNewValue)))
{
Text = strNewValue;
return true;
}
return false;
//throw new NotImplementedException();
}
public void RaisePostDataChangedEvent()
{
//throw new NotImplementedException();
OnTextChanged(E
|