ASP.NET服务器控件开发,如果要实现的是一个单一的控件,不是组合控件(所谓的组合控件是指由两个以上的基本控件组成的复合控件,下面的链接是一篇园子中的前辈写的复合控件事件处理的Blog,参考价值很高
http://www.cnblogs.com/suiqirui19872005/archive/2007/11/05/949708.html
要继承自CompositeControl类,单一控件、包含可视界面的,继承自WebControl类,不需要界面的,例如sqldatasource,继承自Control类)
在单一控件中,button类型的控件,需要回发的是事件,要实现IPostBackEventHandler接口,实现一个方法 public void RaisePostBackEvent(string eventArgument)
{
OnClick(new EventArgs());
}
;text类型的控件,需要回发的是change事件,需要处理回发的数据,要实现IPostBackDataHandler接口,实现两个方法 public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string presentValue = Text;
string postValue = postCollection[postDataKey];
if (presentValue == null || !presentValue.Equals(postValue))
{
Text = postValue;
return true;
}
return false;
}
public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(new EventArgs());
}
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;
namespace ServerControl1
{
[DefaultEvent("Click"),
DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : WebControl,IPostBackEventHandler,IPostBackDataHandler
{
public static readonly object ClickEvent = new object();
public event EventHandler Click
{
add
{
this.Events.AddHandler(ClickEvent, value);
}
remove
{
this.Events.RemoveHandler(ClickEvent, value);
}
}
[Bindable(true)]
public virtual string Text
{
get
{
string s = (string)ViewState["Text"];
return ((s == null) ? string.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input;
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute("Name", this.UniqueID);
//输出控件为提交按钮控件
//writer.AddAttribute("Type", "Submit");
//输出控件为输入控件
writer.AddAttribute("Type", "Text");
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
}
protected virtual void OnClick(EventArgs e)
{
EventHandler clickEventDel = Events[ClickEvent] as EventHandler;
if (clickEventDel != null)
{
clickEventDel(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
OnClick(new EventArgs());
}
protected override void Render(HtmlTextWriter writer)
{
if (Page != null)
{
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer);
}
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string presentValue = Text;
string postValue = postCollection[postDataKey];
if (presentValue == null || !presentValue.Equals(postValue))
{
Text = postValue;
return true;
}
return false;
}
public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(new EventArgs());
}
public event EventHandler TextChanged;
protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged != null)
{
TextChanged(this, e);
}
}
}
}
http://www.cnblogs.com/suiqirui19872005/archive/2007/11/05/949708.html
要继承自CompositeControl类,单一控件、包含可视界面的,继承自WebControl类,不需要界面的,例如sqldatasource,继承自Control类)
在单一控件中,button类型的控件,需要回发的是事件,要实现IPostBackEventHandler接口,实现一个方法 public void RaisePostBackEvent(string eventArgument)
{
OnClick(new EventArgs());
}
;text类型的控件,需要回发的是change事件,需要处理回发的数据,要实现IPostBackDataHandler接口,实现两个方法 public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string presentValue = Text;
string postValue = postCollection[postDataKey];
if (presentValue == null || !presentValue.Equals(postValue))
{
Text = postValue;
return true;
}
return false;
}
public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(new EventArgs());
}
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;
namespace ServerControl1
{
[DefaultEvent("Click"),
DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : WebControl,IPostBackEventHandler,IPostBackDataHandler
{
public static readonly object ClickEvent = new object();
public event EventHandler Click
{
add
{
this.Events.AddHandler(ClickEvent, value);
}
remove
{
this.Events.RemoveHandler(ClickEvent, value);
}
}
[Bindable(true)]
public virtual string Text
{
get
{
string s = (string)ViewState["Text"];
return ((s == null) ? string.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input;
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute("Name", this.UniqueID);
//输出控件为提交按钮控件
//writer.AddAttribute("Type", "Submit");
//输出控件为输入控件
writer.AddAttribute("Type", "Text");
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
}
protected virtual void OnClick(EventArgs e)
{
EventHandler clickEventDel = Events[ClickEvent] as EventHandler;
if (clickEventDel != null)
{
clickEventDel(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
OnClick(new EventArgs());
}
protected override void Render(HtmlTextWriter writer)
{
if (Page != null)
{
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer);
}
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string presentValue = Text;
string postValue = postCollection[postDataKey];
if (presentValue == null || !presentValue.Equals(postValue))
{
Text = postValue;
return true;
}
return false;
}
public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(new EventArgs());
}
public event EventHandler TextChanged;
protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged != null)
{
TextChanged(this, e);
}
}
}
}