首先在VS.NET环境里生成一个UltraTextBoxV1组件(也可以称为自定义控件,我习惯称为组件)项目,
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
//设置该组件的标记前缀
[assembly:TagPrefix("gOODiDEA.UltraTextBoxV1", "UTBV1")]
namespace gOODiDEA.UltraTextBoxV1
{
//添加类声明
[
DefaultProperty("Text"),
ValidationProperty("Text"),
ToolboxData("<{0}:UltraTextBoxV1 runat=server></{0}:UltraTextBoxV1>"),
ParseChildren(false),
Designer("gOODiDEA.UltraTextBoxV1.UltraTextBoxV1Designer")
]
public class UltraTextBoxV1: System.Web.UI.Control, IPostBackDataHandler
{
private static readonly object ValueChangedEvent = new object();
//声明一个代理用于处理值被改变的事件,当组件的值更改时发生ValueChanged事件
public event EventHandler ValueChanged
{
add
{
Events.AddHandler(ValueChangedEvent, value);
}
remove
{
Events.RemoveHandler(ValueChangedEvent, value) ;
}
}
//触发值被改变事件的方法
protected virtual void OnValueChanged(EventArgs e)
{
if( Events != null )
{
EventHandler oEventHandler = ( EventHandler )Events[ValueChangedEvent] ;
if (oEventHandler != null) oEventHandler(this, e);
}
}
//处理回发数据
bool IPostBackDataHandler.LoadPostData( string postDataKey, System.Collections.Specialized.NameValueCollection postCollection )
{
if ( postCollection[postDataKey] != Text )
{
Text = postCollection[postDataKey];
return true;
}
return false;
}
//告诉应用程序该组件的状态已更改
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnValueChanged( EventArgs.Empty );
}
//我们对一个编辑器主要需要实现下面4个属性,Text,Width,Height,BasePath。
//Text属性:(从编辑器得到值和把值赋给编辑器)
[Bindable(true),DefaultValue("")]
public string Text
{
get
{
object o = ViewState["Text"];
return ( o == null ) ? String.Empty : ( string )o;
}
set
{
ViewState["Text"] = value;
}
}
//Width属性:(编辑器的宽)
[Bindable(true),Category("Appearence"),DefaultValue("100%")]
public Unit Width
{
get
{
object o = ViewState["Width"];
return ( o == null ) ? Unit.Parse( "100%" ) : ( Unit )o ;
}
set
{
ViewState["Width"] = value ;
}
}
//Height属性:(编辑器的高)
[Bindable(true),Category("Appearence"),DefaultValue("385px")]
public Unit Height
{
get
{
object o = ViewState["Height"];
return ( o == null ) ? Unit.Parse( "385px" ) : ( Unit )o ;
}
set
{
ViewState["Height"] = value ;
}
}
//BasePath属性:(第一步保存的editor.aspx的路径以及以后做的插件的路径)
[Bindable(true),DefaultValue("../UltraTextBoxV1Sys/Plug-Ins/")]
public string BasePath
{
get
{
object o = ViewState["BasePath"];
return (o == null) ? "../UltraTextBoxV1Sys/Plug-Ins/" : (string)o;
}
set
{
ViewState["BasePath"] = value;
}
}
//接下来是最重要的怎样把本组件和Editor.aspx结合起来,这里使用的是iframe技术:
//覆盖Render方法,运行时输出:
protected override void Render(HtmlTextWriter output)
{
System.Web.HttpBrowserCapabilities oBrowser = Page.Request.Browser ;
//对应的IE版本必须是5.5或以上的版本
if (oBrowser.Browser == "IE" && oBrowser.MajorVersion >= 5.5 && oBrowser.Win32)
{
string sLink = BasePath + "Editor.aspx?FieldName=" + UniqueID;
//如果不使用SetTimeout则会提示找不到对象
output.Write(
"<IFRAME id=\"{5}\" src=\"{0}\" width=\"{1}\" height=\"{2}\" frameborder=\"no\" scrolling=\"no\" οnlοad=\"javascipt:setTimeout('{5}.HtmlEdit.document.body.innerHTML = document.getElementById(\\'{4}
\\').value',1000);\" οnblur=\"{4}.value = {5}.HtmlEdit.document.body.innerHTML\"></IFRAME>",
sLink,
Width,
Height,
Text,
UniqueID,
ID + "_editor"
) ;
//存储编辑器的值
output.Write(
"<INPUT type=\"hidden\" id=\"{0}\" name=\"{0}\" value=\"{1}\" >",
UniqueID,
System.Web.HttpUtility.HtmlEncode(Text) ) ;
}
}
}
//接下来给该组件实现一个设计时的界面:
public class UltraTextBoxV1Designer : System.Web.UI.Design.ControlDesigner
{
public UltraTextBoxV1Designer(){}
public override string GetDesignTimeHtml()
{
UltraTextBoxV1 oControl = ( UltraTextBoxV1 )Component ;
return String.Format(
"<TABLE width=\"{0}\" height=\"{1}\" bgcolor=\"#f5f5f5\" bordercolor=\"#c7c7c7\" cellpadding=\"0\" cellspacing=\"0\" border=\"1\"><TR><TD valign=\"middle\" align=\"center\">UltraTextBox 1.1 - <B>{2}</B></TD></TR></TABLE>",
oControl.Width,
oControl.Height,
oControl.ID ) ;
}
}
}
至此组件部分就基本做完,把它编译后的Dll拷贝你的项目文件夹下,在工具栏-->组件里添加它,你就可以直接拖放进你的页面,在你的工程中使用。
转载于:https://www.cnblogs.com/xyhyt/archive/2007/07/16/819698.html