Handling postback events in a server control (C#) : PostBack « Custom Controls « ASP.NET Tutorial






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, IPostBackEventHandler
    {
        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();
        }

        #region IPostBackEventHandler Members
        public event EventHandler Click;

        public virtual void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            OnClick(EventArgs.Empty);
        }

        #endregion
    
    }
}








14.11.PostBack
14.11.1.Adding PostBack capabilities to a server control (C#)
14.11.2.Adding PostBack capabilities to a server control (VB)
14.11.3.Handling postback events in a server control (C#)
14.11.4.Handling postback events in a server control (VB)