wroxbuttondesigner.cs

来自「ASP.NET服务器控件高级编程电子书」· CS 代码 · 共 77 行

CS
77
字号
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using WroxDesign;

namespace WroxDesign.Design
{
	public class WroxButtonDesigner : ControlDesigner
	{
		public WroxButtonDesigner() 
		{
		}

		public override string GetDesignTimeHtml()
		{
			try
			{
				WroxButton wb = (WroxButton) base.Component;

				if (wb.Text == string.Empty &&
					wb.ImageUrl == string.Empty)
					return GetEmptyDesignTimeHtml();

				Panel pnl = new Panel();
				pnl.CopyBaseAttributes(wb);
				pnl.Controls.Add(wb);
				pnl.BorderStyle = BorderStyle.Dotted;
				pnl.BorderWidth = new Unit(2, UnitType.Point);
				pnl.Height = new Unit(pnl.Height.Value + 4);
				pnl.Width  = new Unit(pnl.Width.Value + 4);
				//Simulate an exception being thrown
				//throw new InvalidOperationException
				//	("My custom exception test.");
				
				return GetRenderHtml(pnl);
			}
			catch (Exception e)
			{
				return GetErrorDesignTimeHtml(e);
			}
		}

		protected override string GetEmptyDesignTimeHtml() 
		{
			//Render design time warning if there's nothing to display.
			string text= "Please set the Text or ImageUrl to initialize the button.";
			return CreatePlaceHolderDesignTimeHtml(text);
		}

		protected override string GetErrorDesignTimeHtml(Exception e)
		{
			string text = string.Format("{0}{1}{2}{3}", 
				"There was an error and the control can't be displayed.",
				"<BR>", "Exception: ", e.Message);

			return CreatePlaceHolderDesignTimeHtml(text);
		}

		/// <summary>
		/// Renders a control to an in-memory string and returns the result.
		/// </summary>
		/// <param name="control">The control to render.</param>
		/// <returns>The HTML representing the rendered control.</returns>
		private string GetRenderHtml(Control control)
		{
			StringWriter text = new StringWriter();
			HtmlTextWriter writer = new HtmlTextWriter(text);
			control.RenderControl(writer);
			return text.ToString();
		}
	}
}

⌨️ 快捷键说明

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