// (1) IPostBackEventHandler interface allows PostBack event processing. [DefaultEvent("Click")] public class AlertButtonThree : System.Web.UI.WebControls.WebControl, IPostBackEventHandler { // (2) Declare postback event public event EventHandler Click; // (3) Render an HTML element that can detect user events. protected override void Render(HtmlTextWriter output) { // Write a title output.Write("<h3>Rendered Control</h3>"); // Add some attributes. output.AddAttribute("value", "Custom Button"); output.AddAttribute("type", "button"); // (4) Add attribute to raise Postback event on client. output.AddAttribute("onclick", "javascript:alert('Howdy!');" + Page.GetPostBackEventReference(this)); // Opens an Input HTML tag (inserts "<INPUT"). output.RenderBeginTag("INPUT"); // Close the Input HTML tag (inserts ">"). output.RenderEndTag(); } // Part of the IPostBackEventHandler interface. // you must create this method. public void RaisePostBackEvent(string eventArgument) { // (5) Call the event method to raise event. OnClick(EventArgs.Empty); } protected virtual void OnClick(EventArgs e) { // Raise the event. if (Click != null) Click(this, e); } }>>Paul,
>using System; >using System.Web.UI; >using System.Web.UI.WebControls; >using System.ComponentModel; > >namespace SampleControls >{ > /// <summary> > /// Summary description for SimpleControl. > /// </summary> > [DefaultProperty("Text"), > ToolboxData("<{0}:kaSimpleControl runat=server></{0}:SimpleControl>")] > public class SimpleControl : System.Web.UI.WebControls.WebControl, IPostBackEventHandler > { > protected string text = "Sample"; > > [Bindable(true), > Category("Appearance"), > DefaultValue("")] > public string Text > { > get > { > return text; > } > > set > { > text = value; > } > } > > protected override void Render(HtmlTextWriter output) > { > string sampleHTML = "<b>" + this.text + "</b>"; > > output.Write(sampleHTML); > } > > public void RaisePostBackEvent(string eventArgument) > { > this.text = "Postback occurred."; > } > } >} > >>
><%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestPostback.WebForm1" ><%@ Register TagPrefix="cc1" Namespace="SampleControls" Assembly="TestPostback" ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > ><HTML> > <HEAD> > <title>WebForm1</title> > <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> > <meta name="CODE_LANGUAGE" Content="C#"> > <meta name="vs_defaultClientScript" content="JavaScript"> > <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> > </HEAD> > <body MS_POSITIONING="GridLayout"> > <form id="Form1" method="post" runat="server"> > <asp:ImageButton id="ImageButton1" style="Z-INDEX: 101; LEFT: 241px; POSITION: absolute; TOP: 207px" runat="server" Width="60px" Height="26px"></asp:ImageButton> > <cc1:SimpleControl id="SimpleControl" runat="server"> </cc1:SimpleControl> > </form> > > </body> ></html> > >>