1. Creare un nuovo progetto di tipo CLASS LIBRARY; 2. Aggiungere la referenza a system.web.dll; 3. Aggiungere la referenza a system.drawing.dll; 4. Referenziare il namespace System.Web.UI.WebControls; 5. Referenziare il namespace System.ComponentModel; 6 Referenziare il namespace System.Drawing; 7. Creare una classe che erediti da WebControl e circondare la classe con le opportune direttive; 8. Implementare un override del metodo RenderContents ereditato dalla classe base utilizzando un oggetto del tipo HtmlTextWriter; 9. Da un’applicazione web, referenziare la dll appena creata e tramite la TOOLBOX di VS aggiungere il webcontrol alla pagina asp.net desiderata. using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; [assembly: TagPrefix("MyControls", "mwc")] namespace MtControls { [ToolboxData("<{0}:MyButton runat=server style=\"position:absolute; z-index:110;\">")] [ToolboxBitmap(typeof(Button))] public class MyButton: WebControl { string ctrlDelimiter = "-"; string _text = "Button"; string _title = ""; string _fnzOnClick = "form_onclick"; int _width = 80; # region Public Properies [Bindable(true)] [Category("MyTool")] public int ButtonWidth { get { return _width; } set { _width = value; } } [Bindable(true)] [Category("MyTool")] public string Text { get { return _text; } set { _text = value; } } [Bindable(true)] [Category("MyTool")] public string Title { get { return _title; } set { _title = value; } } [Bindable(true)] [Category("MyTool")] public string OnClick { get { return _fnzOnClick; } set { _fnzOnClick = value; } } # endregion protected override void RenderContents(HtmlTextWriter output) { //output.AddAttribute(HtmlTextWriterAttribute.Type, "button"); output.AddAttribute(HtmlTextWriterAttribute.Id, "ctrl" + ctrlDelimiter + ID); output.AddAttribute(HtmlTextWriterAttribute.Name, "ctrl" + ctrlDelimiter + ID); output.AddAttribute(HtmlTextWriterAttribute.Value, Text); output.AddAttribute(HtmlTextWriterAttribute.Title, Title); output.AddAttribute(HtmlTextWriterAttribute.Onclick, OnClick +"(this);"); output.AddAttribute(HtmlTextWriterAttribute.Class, "Text Bord Label Button MTC_button"); output.AddAttribute(HtmlTextWriterAttribute.Type, "button"); output.AddAttribute("mt_typ", "F"); output.AddStyleAttribute(HtmlTextWriterStyle.Width, ButtonWidth.ToString() + "px"); output.RenderBeginTag(HtmlTextWriterTag.Input); output.RenderEndTag(); } } }
|