⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 freetextbox.cs

📁 一个基于 Internet Explorer 中 MSHTML 技术的 ASP.NET 服务器控件
💻 CS
📖 第 1 页 / 共 5 页
字号:
		}

		protected override void OnPreRender(EventArgs e) 
		{
			Page.RegisterRequiresPostBack(this);
			this.RegisterClientScript();
			base.OnPreRender(e);
		}
		#endregion

		#region 注册客户端脚本
		protected virtual void RegisterClientScript()
		{
			if (!Page.IsClientScriptBlockRegistered("CopyRightInfo"))
			{
				string info = @"
		<!-- ******************************** -->
		<!-- * FreeTextBox For ASP.NET News * -->
		<!-- * http://wildknight.vicp.net/  * -->
		<!-- * ASP.NET News Controls        * -->
		<!-- ******************************** -->
";
				Page.RegisterClientScriptBlock("CopyRightInfo",info);
			}
			// 按钮脚本
			if (!Page.IsClientScriptBlockRegistered("ToolbarItemsScript"))
			{
				string script = "<script language=\"JavaScript\" src=\"" + ScriptFilePath + "/FTB-ToolbarItems.js\"></script>";
				Page.RegisterClientScriptBlock("FTB-ToolbarItems",script);
			}
			if (IsMshtmlEditorCapable)
			{
				if (!Page.IsClientScriptBlockRegistered("FreeTextBoxMshtmlBaseScript"))
				{
					string script = "<script language=\"JavaScript\" src=\"" + ScriptFilePath + "/FTB-BaseScript.js\"></script>";
					Page.RegisterClientScriptBlock("FreeTextBoxMshtmlBaseScript",script);
				}	
				if (!Page.IsClientScriptBlockRegistered("FreeTextBoxMshtmlStyles"))
				{
					string style = @"
<style>	
	div.FTB_StatusBar {
		text-align: left;
		padding:1px;
		width:100px;
		height: 100%;
		border:1 solid #808080;
		font: 8pt MS Sans Serif;
	}
	select.FTB_Select {
		font: 10pt Arial;
	}
</style>";
					Page.RegisterClientScriptBlock("FreeTextBoxMshtmlStyles",style);
				}
				// Register this control's breakmode script
				Page.RegisterClientScriptBlock(this.ClientID + "_BreakMode", BreakModeScript());
				// Register this control's pasting features
				Page.RegisterClientScriptBlock(this.ClientID + "_OnPaste", OnPasteScript());
				// Register this control's button styles
				Page.RegisterClientScriptBlock(this.ClientID + "EditorStyles", EditorStyles());

				string ButtonPreload = "";
				string ButtonPrefix = "";
				ButtonPrefix = this.ButtonPath.Substring(0,ButtonPath.Length - 1);
				ButtonPrefix = ButtonPrefix.Substring(ButtonPrefix.LastIndexOf("/") + 1,ButtonPrefix.Length - ButtonPrefix.LastIndexOf("/") - 1);

				if (this.ButtonOverImage)
				{
					ButtonPreload += "img_" + ButtonPrefix + "_ButtonOverImage = new Image();\n";
					ButtonPreload += "img_" + ButtonPrefix + "_ButtonOverImage.src = \"" + ButtonPath + "toolbarbutton.over." + this.ButtonFileExtention + "\";\n";
				}
				if (this.ButtonDownImage)
				{
					ButtonPreload += "img_" + ButtonPrefix + "_ButtonDownImage = new Image();\n";
					ButtonPreload += "img_" + ButtonPrefix + "_ButtonDownImage.src = \"" + ButtonPath + "toolbarbutton.down." + this.ButtonFileExtention + "\";\n";
				}
				for (int i = 0; i < Toolbars.Count; i++)
				{
					for (int j = 0; j < Toolbars[i].Items.Count; j++)
					{
						ToolbarItem myItem = Toolbars[i].Items[j];
						// Client script generation for new postback button control
						if (myItem.GetType() == typeof (FreeTextBoxControls.ToolbarPostBackButton))
						{
							ToolbarPostBackButton myButton = (ToolbarPostBackButton) myItem;

							if (!Page.IsClientScriptBlockRegistered(myButton.Function))
							{
								Page.RegisterClientScriptBlock(myButton.Function, myButton.ScriptBlock);
							}
							ButtonPreload +=  "img_" + ButtonPrefix + "_" + myButton.Name + " = new Image();\n";
							ButtonPreload += "img_" + ButtonPrefix + "_" + myButton.Name + ".src = \"" + ButtonPath + myButton.Name.ToLower() + "." + this.ButtonFileExtention + "\";\n";
							if (ButtonOverImage)
							{
								ButtonPreload +=  "img_" + ButtonPrefix + "_" + myButton.Name + "_over = new Image();\n";
								ButtonPreload += "img_" + ButtonPrefix + "_" + myButton.Name + "_over.src = \"" + ButtonPath + myButton.Name.ToLower() + ".over." + this.ButtonFileExtention + "\";";
							}
						}
						else if (myItem.GetType() == typeof (FreeTextBoxControls.ToolbarButton))
						{
							ToolbarButton myButton = (ToolbarButton) myItem;

							if (!Page.IsClientScriptBlockRegistered(myButton.Function))
							{
								Page.RegisterClientScriptBlock(myButton.Function, myButton.ScriptBlock);
							}
							ButtonPreload +=  "img_" + ButtonPrefix + "_" + myButton.Name + " = new Image();\n";
							ButtonPreload += "img_" + ButtonPrefix + "_" + myButton.Name + ".src = \"" + ButtonPath + myButton.Name.ToLower() + "." + this.ButtonFileExtention + "\";\n";
							if (ButtonOverImage)
							{
								ButtonPreload +=  "img_" + ButtonPrefix + "_" + myButton.Name + "_over = new Image();\n";
								ButtonPreload += "img_" + ButtonPrefix + "_" + myButton.Name + "_over.src = \"" + ButtonPath + myButton.Name.ToLower() + ".over." + this.ButtonFileExtention + "\";";
							}
						}
						else if (myItem.GetType() == typeof(FreeTextBoxControls.ToolbarDropDownList))
						{
							ToolbarDropDownList myDropDown = (ToolbarDropDownList) myItem;
							if (!Page.IsClientScriptBlockRegistered(myDropDown.Function))
							{
								Page.RegisterClientScriptBlock(myDropDown.Function, myDropDown.ScriptBlock);
							}
						}
						else
						{
							// Separator
						}
					}
					// each item
				}
				// each toolbar
				ButtonPreload = @"
<script language=""JavaScript"">
" + ButtonPreload + @"
</script>";

				if (!this.Page.IsClientScriptBlockRegistered(ButtonPrefix + "_images"))
				{
					this.Page.RegisterClientScriptBlock(ButtonPrefix + "_images", ButtonPreload);
				}

				// fix for IE 5 which doesn't not have iframe.onblur
				this.Page.RegisterOnSubmitStatement(this.ClientID + "_OnSubmit","FTB_CopyHtmlToHidden(" + this.ClientID + @"_editor,document.getElementById('" + this.ClientID + @"')," + this.ClientID + @"_HtmlMode);");

			}
			else if (this.IsBasicEditorCapable && this.DownlevelMode == DownlevelMode.BasicEditor)
			{
				this.StartMode = EditorMode.HtmlMode;
				if (!Page.IsClientScriptBlockRegistered("FreeTextBoxBasicEditorBase"))
				{
					string script = "<script language=\"JavaScript\" src=\"" + ScriptFilePath + "/FTB-BasicEditorBase.js\"></script>";
					Page.RegisterClientScriptBlock("FreeTextBoxBasicEditorBase", script);
				}
			}
		}
		#endregion

		#region 制表符号处理
		private string BreakModeScript()
		{
			string html = @"
<script language=""JavaScript"">
function FTB_" + this.ClientID + @"_onKeyDown_" + this.BreakMode.ToString() + @"() {
	editor = " + this.ClientID + @"_editor;
	var _TAB = 9;
	var _ENTER = 13;
	var _QUOTE = 222;
	var _OPENCURLY = '&#8220;';
	var _CLOSECURLY = '&#8221;';

	if (editor.event.keyCode == _TAB && editor.event.ctrlKey) {
		//HtmlMode to DesignMode (Ctrl+TAB)
		if (" + this.ClientID + @"_HtmlMode && editor.event.keyCode == _TAB && editor.event.ctrlKey) {
			FTB_SetActiveTab(document.getElementById('" + this.ClientID + @"_DesignModeTab','" + this.ClientID + @"'));
			" + this.ClientID + @"_ChangeMode(editor,document.getElementById('" + this.ClientID + @"_TempFrame'),'" + this.ClientID + @"_Toolbar',false," + this.AutoHideToolbar.ToString().ToLower( ) + @");
		}
		//DesignMode to HtmlMode (Ctrl+TAB)
		if (!" + this.ClientID + @"_HtmlMode && editor.event.keyCode == _TAB && editor.event.ctrlKey) {
			FTB_SetActiveTab(document.getElementById('" + this.ClientID + @"_HtmlModeTab'),'" + this.ClientID + @"');
			" + this.ClientID + @"_ChangeMode(editor,document.getElementById('" + this.ClientID + @"_TempFrame'),'" + this.ClientID + @"_Toolbar',true," + this.AutoHideToolbar.ToString().ToLower( ) + @");
		}
		" + this.ClientID + @"_HtmlMode = !" + this.ClientID + @"_HtmlMode;
	}";
			if (EnableCurlyQuotes) 
			{
				html += @"
	if (editor.event.keyCode == _QUOTE && editor.event.shiftKey && !" + this.ClientID + @"_HtmlMode) {
		var sel = editor.document.selection;
		if (sel.type == 'Control') return;
		var r = sel.createRange();
		var before = FTB_CharBefore(r);
		var after = FTB_CharAfter(r);
		var r = sel.createRange();

		if (before == 'start') {
			r.pasteHTML(_OPENCURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		} else if (before != ' ' && after == 'end') {
			r.pasteHTML(_CLOSECURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		} else if (before == ' ' && after == 'end') {
			r.pasteHTML(_OPENCURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		} else if (before != ' ' && after == ' ') {
			r.pasteHTML(_CLOSECURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		} else {
			r.pasteHTML(_OPENCURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		}
	}";
			}

			// TAB Functions
			if (TabMode == TabMode.Disabled)
			{
				html += @"
	if (editor.event.keyCode == _TAB) {
		editor.event.cancelBubble = true;
		editor.event.returnValue = false;
	}";
			}
			else if (TabMode == TabMode.Default)
			{
				html += @"
	if (editor.event.keyCode == _TAB) {
		var sel = editor.document.selection;
		var r = sel.createRange();
		r.pasteHTML('&nbsp;&nbsp;&nbsp;&nbsp;');
		editor.event.cancelBubble = true;
		editor.event.returnValue = false;
	}";
			}
			else
			{
				// do nothing for TabMode.NextControl
			}
				
			if (BreakMode == BreakMode.LineBreak) 
			{
				html += @"
	if (editor.event.keyCode == _ENTER) {
		var sel = editor.document.selection;
		if (sel.type == 'Control') {
			return;
		}
		var r = sel.createRange();
		if ((!FTB_CheckTag(r.parentElement(),'LI'))&&(!FTB_CheckTag(r.parentElement(),'H'))) {
			r.pasteHTML('<br>');
			editor.event.cancelBubble = true; 
			editor.event.returnValue = false; 
			r.select();
			r.collapse(false);
			return false;
		}
	}";
			}
			else
			{
				html += @"
	if ((" + this.ClientID + @"_HtmlMode && editor.event.keyCode == _ENTER) || (!" + this.ClientID + @"_HtmlMode && editor.event.ctrlKey && editor.event.keyCode == _ENTER)) {
		var sel = editor.document.selection;
		if (sel.type == 'Control') {
			return;
		}
		var r = sel.createRange();
		if ((!FTB_CheckTag(r.parentElement(),'LI'))&&(!FTB_CheckTag(r.parentElement(),'H'))) {
			r.pasteHTML('<br>');
			editor.event.cancelBubble = true; 
			editor.event.returnValue = false; 
			r.select();
			r.collapse(false);
			return false;
		}
	}";
			}

			html += @"
}
</script>";
			return html;
		}
		#endregion

		#region 处理粘贴字符串
		private string OnPasteScript()
		{
			string html = "";
			switch (this.PasteMode) 
			{
				case PasteMode.Disabled:
					html += @"
<script language=""JavaScript"">
function FTB_" + this.ClientID + @"_onPaste() {
	alert('Pasting is disabled');
	return false;
}
</script>";
					break;
				case PasteMode.NoHtml:
					html += @"
<script language=""JavaScript"">
function FTB_" + this.ClientID + @"_onPaste() {
	var text = window.clipboardData.getData('Text');
	text = text.replace(/<[^>]*>/gi,'');
	" + this.ClientID + @".focus();
	s = " + this.ClientID + @".document.selection.createRange();
	s.pasteHTML(text); 
	return false;
}
</script>";
					break;
				case PasteMode.NoScript:
					html += @"
<script language=""JavaScript"">
function FTB_" + this.ClientID + @"_onPaste() {
	setTimeout(FTB_" + this.ClientID + @"_Delayed,10)
}
function FTB_" + this.ClientID + @"_Delayed() {
	editor = eval(" + this.ClientID + @"_editor);
	var html = editor.document.body.innerHTML;
	html = html.replace(/<script[^>]*>[\w|\t|\r|\W]*<\/script>/gi,'');
	editor.document.body.innerHTML = html;
}
</script>";
					break;
				default:
					html += @"
<script language=""JavaScript"">
function FTB_" + this.ClientID + @"_onPaste() {
	// return;
}
</script>";
					break;
			}
			return html;
		}
		#endregion

		#region 编辑器样式
		private string EditorStyles() 
		{
			string html = @"
<style>
td." + this.CssID + @"_StartTabOn {
	font: 10pt MS Sans Serif;
	padding: 1px;
	border-left: 1px solid " + ColorTranslator.ToHtml(this.GutterBackColor) + @";
	border-right: 1px solid " + ColorTranslator.ToHtml(this.GutterBorderColorLight) + @";
	border-top: 1px solid " + ColorTranslator.ToHtml(this.GutterBorderColorDark) + @";
	border-bottom: 1px solid " + ColorTranslator.ToHtml(this.GutterBackColor) + @";
	background-color: " + ColorTranslator.ToHtml(this.GutterBackColor) + @";
}
td." + this.CssID + @"_StartTabOff {
	font: 10pt MS Sans Serif;
	padding:1px;
	border-left: 1px solid " + ColorTranslator.ToHtml(this.GutterBackColor) + @";
	border-right: 1px solid " + ColorTranslator.ToHtml(this.GutterBorderColorDark) + @";
	border-top: 1px solid " + ColorTranslator.ToHtml(this.GutterBorderColorDark) + @";
	border-bottom: 1px solid " + ColorTranslator.ToHtml(this.GutterBackColor) + @";
	background-color: " + ColorTranslator.ToHtml(this.GutterBackColor) + @";
}
td." + this.CssID + @"_TabOn {
	font: 8pt MS Sans Serif;
	padding:1px;
	padding-left:5px;
	padding-right:5px;
	border-left: 1px solid " + ColorTranslator.ToHtml(this.GutterBorderColorLight) + @";
	border-right: 1px solid " + ColorTranslator.ToHtml(this.GutterBorderColorDark) + @";
	border-top: 1px solid " + ColorTranslator.ToHtml(this.BackColor) + @";
	b

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -