ASP.NET服务器控件开发
2009-01-09 13:42 Virus-BeautyCode 阅读(540) 评论(0) 编辑 收藏 举报 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);
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构