Mega Code Archive

 
Categories / ASP.Net Tutorial / Custom Controls
 

Adding PostBack capabilities to a server control (C#)

using System.ComponentModel; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; namespace WebControlLibrary1 {     [DefaultProperty("Text")]     [ToolboxData("<{0}:WebCustomControl3 runat=server></{0}:WebCustomControl3>")]     public class WebCustomControl3 : WebControl     {         protected override void Render(HtmlTextWriter output)         {             PostBackOptions p = new PostBackOptions(this);             output.AddAttribute(HtmlTextWriterAttribute.Onclick,                 Page.ClientScript.GetPostBackEventReference(p));             output.AddAttribute(HtmlTextWriterAttribute.Value, "My Button");             output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);             output.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);             output.RenderBeginTag(HtmlTextWriterTag.Button);             output.RenderEndTag();         }     } }