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.Collections.Specialized;
namespace ServerControl
{
[DefaultProperty("Text")]
[DefaultEvent("TextChanged")]
[ToolboxData("<{0}:KingTextBoxCanPostevent runat=server></{0}:KingTextBoxCanPostevent>")]
public class KingTextBoxCanPostevent : Control,IPostBackDataHandler,IRaiseItemChangedEvents
{
public KingTextBoxCanPostevent() { }
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"]=value;
}
}
public bool AutoBostBack
{
get;
set;
}
protected override void OnPreRender(EventArgs e)
{
PostBackOptions pbo = new PostBackOptions(this);
pbo.AutoPostBack = AutoBostBack;
pbo.PerformValidation = true;
pbo.TrackFocus = true;
pbo.ClientSubmit = true;
pbo.RequiresJavaScriptProtocol = false;
string strPostBackCode = this.Page.ClientScript.GetPostBackEventReference(pbo);
StringBuilder strPostBackFromClient = new StringBuilder();
strPostBackFromClient.Append(" function PostBackFromClient_"+this.ClientID+"()");
strPostBackFromClient.Append("{");
strPostBackFromClient.Append(strPostBackCode+";");
strPostBackFromClient.Append("}");
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "PostBackFromClient_" + this.ClientID))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"PostBackFromClient_"+this.ClientID,strPostBackFromClient.ToString(),true);
}
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
//base.Render(writer);
StringBuilder sb = new StringBuilder();
sb.Append("<input type=\"text\" name=");
sb.Append("\""+this.UniqueID+"\"");
sb.Append(" value=");
sb.Append("\"" + HttpUtility.HtmlEncode(Text)+ "\"");
sb.Append(" οnblur='" + "PostBackFromClient_" + this.ClientID + "();'");
sb.Append(" />");
writer.Write(sb.ToString());
}
public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string strOldValue = Text;
string strNewValue = postCollection[this.UniqueID];
if (strOldValue == null || (strOldValue != null && !strOldValue.Equals(strNewValue)))
{
this.Text = strNewValue;
return true;
}
return false;
}
public event EventHandler TextChanged;
protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged != null)
{
TextChanged(this, e);
}
}
#region IRaiseItemChangedEvents Members
public bool RaisesItemChangedEvents
{
get { throw new NotImplementedException(); }
}
#endregion
#region IPostBackDataHandler Members
public void RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}
#endregion
}
}
-------------------------------------------------------------------------------------
复合控件的事件处理机制:
private static readonly object TextChangedKeyObject = new object();
public event EventHandler TextChanged2
{
add
{
base.Events.AddHandler(TextChangedKeyObject, value);
}
remove
{
base.Events.RemoveHandler(TextChangedKeyObject, value);
}
}
protected virtual void OnTextChanged2(EventArgs e)
{
EventHandler handler = base.Events[TextChangedKeyObject] as EventHandler;
if (handler != null)
{
handler(this, e);
}
}