asp.net Web编辑器
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Text;
using
System.Drawing;
/**/
/// <summary>/// WebEditor 的摘要说明/// </summary>
namespace
ControlLibrary
...
{ public class WebEditor : WebControl, IPostBackDataHandler ...{ public WebEditor() ...{ } public String Text ...{ get ...{ string text1 = (string)this.ViewState["Text"]; if (text1 != null) ...{ return text1; } return string.Empty; } set ...{ this.ViewState["Text"] = value; } } public String Value ...{ get ...{ string value1 = (string)this.ViewState["Value"]; if (value1 != null) ...{ return value1; } return string.Empty; } set ...{ this.ViewState["Value"] = value; } } public virtual bool CausesValidation ...{ get ...{ object obj1 = this.ViewState["CausesValidation"]; if (obj1 != null) ...{ return (bool)obj1; } return false; } set ...{ this.ViewState["CausesValidation"] = value; } } public virtual string ValidationGroup ...{ get ...{ string text1 = (string)this.ViewState["ValidationGroup"]; if (text1 != null) ...{ return text1; } return string.Empty; } set ...{ this.ViewState["ValidationGroup"] = value; } } IPostBackDataHandler 成员#region IPostBackDataHandler 成员 public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection) ...{ String[] presentValue = new string[]...{Text,Value}; String[] postedValue = postCollection.GetValues(postDataKey); if (presentValue == null || !presentValue.Equals(postedValue)) ...{ Text = postedValue[0]; Value = postedValue[1]; return true; } return false; } public void RaisePostDataChangedEvent() ...{ if (this.CausesValidation) ...{ this.Page.Validate(this.ValidationGroup); } } #endregion protected override object SaveViewState() ...{ if (!this.SaveTextViewState) ...{ this.ViewState.SetItemDirty("Text", false); } return base.SaveViewState(); } private bool SaveTextViewState ...{ get ...{ return true; } } protected override void Render(HtmlTextWriter writer) ...{ string txt = Text; string frameID = UniqueID + "_Editor"; string dlgID = UniqueID + "_dlgHelper"; txt = txt.Replace("/'", "&^7'"); txt = txt.Replace("/'", "&^5'"); txt = txt.Replace("'", "&^3'"); txt = txt.Replace("'", "'"); txt = txt.Replace("&^7'", "///'"); txt = txt.Replace("&^5'", "//'"); txt = txt.Replace("&^3'", "/'"); txt = txt.Replace(" ", string.Empty); txt = txt.Replace(" ", string.Empty); StringBuilder sb = new StringBuilder(); sb.AppendFormat("<OBJECT id="{0}" CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" width = '0px' height = '0px'></OBJECT>", dlgID); sb.Append("<table class="toolbar" cellspacing=1 cellpadding=1><tr><td>"); sb.AppendFormat("<img οnclick="if(confirm('所有内容将被清空,确定继续吗?')){{window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('refresh');}}" HIDEFOCUS style="cursor:hand" title="新建" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.new.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('saveas','false','WebEditorDoc');" HIDEFOCUS style="cursor:hand" title="保存" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.save.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('print');" HIDEFOCUS style="cursor:hand" title="打印" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.print.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img src="{0}">", Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.separator.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('cut');" HIDEFOCUS style="cursor:hand" title="剪切" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.cut.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('copy');" HIDEFOCUS style="cursor:hand" title="复制" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.copy.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('paste');" HIDEFOCUS style="cursor:hand" title="粘贴" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.paste.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('removeformat');" HIDEFOCUS style="cursor:hand" title="重置样式" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.reset.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img src="{0}">", Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.separator.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('createlink');" HIDEFOCUS style="cursor:hand" title="链接" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.anchor.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('insertimage');" HIDEFOCUS style="cursor:hand" title="图片" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.img.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();var range = window.frames['{0}'].document.selection.createRange();range.pasteHTML('<fieldset><legend>标题</legend></fieldset>');" HIDEFOCUS style="cursor:hand" title="栏目框" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.fieldset.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('inserthorizontalrule');" HIDEFOCUS style="cursor:hand" title="水平分割线" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.hr.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();var range = window.frames['{0}'].document.selection.createRange();range.pasteHTML('<br>');" HIDEFOCUS style="cursor:hand" title="换行" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.br.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img src="{0}">", Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.separator.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="var d = new Date();window.frames['{0}'].focus();var range = window.frames['{0}'].document.selection.createRange();range.text=d.getYear()+'年'+(parseInt(d.getMonth())+1)+'月'+d.getDate()+'日'"" HIDEFOCUS style="cursor:hand" title="插入当前日期" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.time.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('insertmarquee');" HIDEFOCUS style="cursor:hand" title="滚动效果" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.marquee.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();var range = window.frames['{0}'].document.selection.createRange();range.pasteHTML('<object classid=/'CLSID:0002E510-0000-0000-C000-000000000046/'><param name=/'DisplayTitleBar/' value=/'false/'/><param name=/'DataType/' value=/'HTMLData/'/><param name=/'HTMLData/' value=/'htmltable/'/></object>');" HIDEFOCUS style="cursor:hand" title="插入Excel" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.excel.gif")); sb.Append("</td><td colspan='5'>"); sb.AppendFormat("<img οnclick="this.parentNode.childNodes[1].style.display='';" HIDEFOCUS style="cursor:hand" title="插入媒体" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.media.gif")); sb.AppendFormat("<input type="text" style="position:absolute;font-size:11px;width:100%;display:none" οnkeypress="if(event.keyCode=='13'){{window.frames['{0}'].focus(); var range = window.frames['{0}'].document.selection.createRange();range.pasteHTML('<embed src=/'' + this.value + '/' autostart=/'true/' loop=/'true/'>');this.style.display='none'}}">", frameID); sb.Append("</td></tr><tr><td colspan='8'>"); sb.AppendFormat("<SELECT style="font-size:9px" οnchange="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('formatblock','false',this.value);" ><option value="">段落</option><option value="<P>">普通<option value="<H1>">标题一<option value="<H2>">标题二<option value="<H3>">标题三<option value="<H4>">标题四<option value="<H5>">标题五<option value="<H6>">标题六</SELECT>", frameID); sb.AppendFormat("<SELECT style="font-size:9px" οnchange="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('fontname','false',this.value);" ><option value="">字体</option>", frameID); foreach (FontFamily f in FontFamily.Families) ...{ string name = f.Name; if (f.Name.Length > 3) ...{ name = f.Name.Substring(0, 3); } sb.AppendFormat("<option value="{0}">{1}</option>", f.Name, name); } sb.AppendFormat("</SELECT>"); sb.AppendFormat("<SELECT style="font-size:9px" οnchange="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('formatblock','false',this.value);" ><option value="">字号</option><option value="7">一号<option value="6">二号<option value="5">三号<option value="4">四号<option value="3">五号<option value="2">六号<option value="1">七号</SELECT>", frameID); sb.Append("</td><td>"); sb.AppendFormat("<img src="{0}">", Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.separator.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('bold');" HIDEFOCUS style="cursor:hand" title="粗体" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.bold.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('italic');" HIDEFOCUS style="cursor:hand" title="斜体" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.italic.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('underline');" HIDEFOCUS style="cursor:hand" title="下划线" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.underline.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img src="{0}">", Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.separator.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('justifyleft');" HIDEFOCUS style="cursor:hand" title="左对齐" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.align-left.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('justifycenter');" HIDEFOCUS style="cursor:hand" title="居中" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.align-center.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('justifyright');" HIDEFOCUS style="cursor:hand" title="右对齐" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.align-right.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img src="{0}">", Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.separator.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('insertorderedlist');" HIDEFOCUS style="cursor:hand" title="有序列表" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.ol.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('insertorderedlist');" HIDEFOCUS style="cursor:hand" title="无序列表" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.ul.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('outdent');" HIDEFOCUS style="cursor:hand" title="减少缩进" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.outdent.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="window.frames['{0}'].focus();window.frames['{0}'].document.execCommand('indent');" HIDEFOCUS style="cursor:hand" title="增加缩进" src='{1}'>", frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.indent.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img src="{0}">", Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.separator.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="var color = document.all['{0}'].ChooseColorDlg().toString(16);color=((color.length<6)?'000000'.substring(0,6-color.length):'')+color;window.frames['{1}'].focus();window.frames['{1}'].document.execCommand('forecolor','false',color);" HIDEFOCUS style="cursor:hand" title="字体颜色" src='{2}'>", dlgID, frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.forecolor.gif")); sb.Append("</td><td>"); sb.AppendFormat("<img οnclick="var color = document.all['{0}'].ChooseColorDlg().toString(16);color=((color.length<6)?'000000'.substring(0,6-color.length):'')+color;window.frames['{1}'].focus();window.frames['{1}'].document.execCommand('backcolor','false',color);" HIDEFOCUS style="cursor:hand" title="背景颜色" src='{2}'>", dlgID, frameID, Page.ClientScript.GetWebResourceUrl(typeof(WebEditor), "ControlLibrary.images.backcolor.gif")); sb.Append("</td></tr></table>"); writer.Write(sb.ToString()); writer.Write("<div style="border:1px solid skyblue;height:300px;width:500px">"); string html = "<IFRAME id="{0}" name="{0}" width="100%" height="100%" onload ="window.frames['{0}'].document.designMode='On';" οnblur="document.all['{1}_1'].value = window.frames['{0}'].document.body.innerHTML;document.all['{1}_2'].value = window.frames['{0}'].document.body.innerText"></IFRAME><script>window.frames['{0}'].document.open();window.frames['{0}'].document.write('<body>{2}</body>');window.frames['{0}'].document.close();</script>"; writer.Write(html, frameID, UniqueID, txt); writer.Write("</div>"); writer.Write( "<INPUT type="hidden" id="{0}_1" name="{0}" value="{1}" >", UniqueID, HttpUtility.HtmlEncode(Text)); writer.Write( "<INPUT type="hidden" id="{0}_2" name="{0}" value="{1}" >", UniqueID, Value); } }}
转载请注明原文地址: https://ibbs.8miu.com/read-14622.html