
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
//設置該組件的標記前綴
[assembly:TagPrefix("gOODiDEA.UltraTextBoxV1", "UTBV1")]
namespace gOODiDEA.UltraTextBoxV1
{
//添加類(lèi)聲明
[
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();
//聲明一個(gè)代理用于處理值被改變的事件,當組件的值更改時(shí)發(fā)生ValueChanged事件
public event EventHandler ValueChanged
{
add
{
Events.AddHandler(ValueChangedEvent, value);
}
remove
{
Events.RemoveHandler(ValueChangedEvent, value) ;
}
}
//觸發(fā)值被改變事件的方法
protected virtual void OnValueChanged(EventArgs e)
{
if( Events != null )
{
EventHandler oEventHandler = ( EventHandler )Events[ValueChangedEvent] ;
if (oEventHandler != null) oEventHandler(this, e);
}
}
//處理回發(fā)數據
bool IPostBackDataHandler.LoadPostData( string postDataKey, System.Collections.Specialized.NameValueCollection postCollection )
{
if ( postCollection[postDataKey] != Text )
{
Text = postCollection[postDataKey];
return true;
}
return false;
}
//告訴應用程序該組件的狀態(tài)已更改
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnValueChanged( EventArgs.Empty );
}
//我們對一個(gè)編輯器主要需要實(shí)現下面4個(gè)屬性,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;
}
}
//接下來(lái)是最重要的怎樣把本組件和Editor.aspx結合起來(lái),這里使用的是iframe技術(shù):
//覆蓋Render方法,運行時(shí)輸出:
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則會(huì )提示找不到對象
output.Write(
"<IFRAME id=\"{5}\" src=\"{0}\" width=\"{1}\" height=\"{2}\" frameborder=\"no\" scrolling=\"no\" onload=\"javascipt:setTimeout(‘{5}.HtmlEdit.document.body.innerHTML = document.getElementById(\\‘{4}
\\‘).value‘,1000);\" onblur=\"{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) ) ;
}
}
}
//接下來(lái)給該組件實(shí)現一個(gè)設計時(shí)的界面:
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 ) ;
}
}
}
<div class="Btn" TITLE="上傳圖片" LANGUAGE="javascript" onclick="UTB_InsertImg()">
<img class="Ico" src="..\images\img.gif" WIDTH="16" HEIGHT="16">
</div>
<div class="Btn" TITLE="插入EXCEL表格" LANGUAGE="javascript" onclick="UTB_InsertExcel()">
<img class="Ico" src="..\images\insertexcel.gif" WIDTH="16" HEIGHT="16">
</div>
然后在JScript代碼里添加UTB_InsertImg(),UTB_InsertExcel()的實(shí)現:
function UTB_InsertImg()
{
//只能在編輯模式下使用
if ( ! UTB_validateMode() )
return;
HtmlEdit.focus();
//在當前光標處創(chuàng )建一個(gè)區域用于插入圖片
var range = HtmlEdit.document.selection.createRange();
//用模式對話(huà)框打開(kāi)上傳頁(yè)面,把返回值插入到編輯器中
var arr = showModalDialog(""uploadface.aspx"", """", ""dialogWidth:430px;dialogHeight:280px;help:0;status:0"");
if (arr != null)
{
//得到的返回值應該是形如:<img src="http://61.139.77.178:8088/gOODiDEA/pic01.jpg">
range.pasteHTML( arr );
}
HtmlEdit.focus();
}
function UTB_InsertExcel()
{
if (!UTB_validateMode())
return;
HtmlEdit.focus();
//在這里其實(shí)就是插入一個(gè)Microsoft Office Web Components(MSOWC)組件
var range = HtmlEdit.document.selection.createRange();
range.pasteHTML(""<object classid=‘clsid:0002E510-0000-0000-C000-000000000046‘ id=‘Spreadsheet1‘ codebase=‘file:\\Bob\software\office2000\msowc.cab‘ width=‘100%‘ height=‘250‘><param name=‘HTMLURL‘ value><param name=‘HTMLData‘ value=‘<html xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns="http://www.w3.org/TR/REC-html40"><head><style type="text/css"><!--tr{mso-height-source:auto;}td{black-space:nowrap;}.wc4590F88{black-space:nowrap;font-family:宋體;mso-number-format:General;font-size:auto;font-weight:auto;font-style:auto;text-decoration:auto;mso-background-source:auto;mso-pattern:auto;mso-color-source:auto;text-align:general;vertical-align:bottom;border-top:none;border-left:none;border-right:none;border-bottom:none;mso-protection:locked;}--></style></head><body><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:OWCVersion>9.0.0.2710</x:OWCVersion><x:Label Style=‘border-top:solid .5pt silver;border-left:solid .5pt silver;border-right:solid .5pt silver;border-bottom:solid .5pt silver‘><x:Caption>Microsoft Office Spreadsheet</x:Caption> </x:Label><x:Name>Sheet1</x:Name><x:WorksheetOptions><x:Selected/><x:Height>7620</x:Height>""+
""<x:Width>15240</x:Width><x:TopRowVisible>0</x:TopRowVisible><x:LeftColumnVisible>0</x:LeftColumnVisible> <x:ProtectContents>False</x:ProtectContents> <x:DefaultRowHeight>210</x:DefaultRowHeight> <x:StandardWidth>2389</x:StandardWidth> </x:WorksheetOptions> </x:ExcelWorksheet></x:ExcelWorksheets> <x:MaxHeight>80%</x:MaxHeight><x:MaxWidth>80%</x:MaxWidth></x:ExcelWorkbook></xml><![endif]--><table class=wc4590F88 x:str><col width="56"><tr height="14"><td></td></tr></table></body></html>‘> <param name=‘DataType‘ value=‘HTMLDATA‘> <param name=‘AutoFit‘ value=‘0‘><param name=‘DisplayColHeaders‘ value=‘-1‘><param name=‘DisplayGridlines‘ value=‘-1‘><param name=‘DisplayHorizontalScrollBar‘ value=‘-1‘><param name=‘DisplayRowHeaders‘ value=‘-1‘><param name=‘DisplayTitleBar‘ value=‘-1‘><param name=‘DisplayToolbar‘ value=‘-1‘><param name=‘DisplayVerticalScrollBar‘ value=‘-1‘> <param name=‘EnableAutoCalculate‘ value=‘-1‘> <param name=‘EnableEvents‘ value=‘-1‘><param name=‘MoveAfterReturn‘ value=‘-1‘><param name=‘MoveAfterReturnDirection‘ value=‘0‘><param name=‘RightToLeft‘ value=‘0‘><param name=‘ViewableRange‘ value=‘1:65536‘></object>"");
HtmlEdit.focus();
}
聯(lián)系客服