理解button的Click事件和TextBox的TextChanged\DropDownList.SelectedIndexChanged的实现原理的区别
using System;
using System.Web.UI;
using System.ComponentModel;
namespace control
{
/// <summary>
/// 理解button的Click事件和TextBox的TextChanged\DropDownList.SelectedIndexChanged的区别
/// </summary>
public class control1 : Control,IPostBackEventHandler,IPostBackDataHandler
{
public control1()
{
}
private string name;
[BrowsableAttribute(true)]
[DescriptionAttribute("姓名")]
[DefaultValueAttribute("自定义名称")]
[CategoryAttribute("aaa")]
public virtual string PersonName
{
set
{
this.name=value;
}
get
{
return this.name;
}
}
/// <summary>
/// 老数据保存在ViewState中,如果禁用了ViewState则回传数据将失效
/// </summary>
public string Text
{
get
{
object text = ViewState["Text"];
if (text == null)
return string.Empty;
else
return (string)text;
}
set
{
ViewState["Text"] = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<INPUT TYPE=submit name="+this.UniqueID+" Value='确定' /");
//writer.Write("<INPUT TYPE='text' name="+this.UniqueID+" Value='"+this.Text+"' /");
base.Render(writer);
}
#region 事件回传
// public delegate void EventHandler(Object sender, EventArgs e);
// public event EventHandler Click;
public event EventHandler Click;//相当于上面两句
protected virtual void OnMxhClick(System.EventArgs e)
{
if(Click!=null)
{
Click(this,e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
OnMxhClick(EventArgs.Empty);
}
#endregion
#region 数据回传
/// <summary>
/// 判断是否调用RaisePostDataChangedEvent()
/// </summary>
/// <param name="postDataKey">传回来的新数据</param>
/// <param name="postCollection"></param>
/// <returns></returns>
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string postData=postCollection[postDataKey];
if(!Text.Equals(postData))
{
Text=postData;
return true;//不同则自动调用RaisePostDataChangedEvent()函数
}
else
{
return false;
}
}
public void RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}
public event EventHandler TextChanged;
public void OnTextChanged(EventArgs e)
{
if(TextChanged!=null)
{
TextChanged(this,e);
}
}
#endregion
}
}