Hello Developers,
How do create a custom WebControl (not UserControl), exposing OnClick event?
I reviewed countless examples found by Google, but came across nothing
helpful.
Below I attach what I got so far.
Thank you for any hints.
Tomasz J
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
[DefaultProperty ("Text")]
[ToolboxData("<{ 0}:MyButton runat=\"server\ "></{0}:MyCloseButt on>")]
public class MyButton : WebControl
{
[Bindable(true)]
[Category("Appea rance")]
[DefaultValue("" )]
[Localizable(fal se)]
public string Text
{
get
{
String s = (String)ViewSta te["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
// should I use: Attributes.Add( "OnClick",
Page.Clientscri pt.GetPostBackE ventReference(t his, ID.ToString))
[Category("New") , Browsable(true)]
public event EventHandler Click;
protected void OnClick(EventAr gs e)
{
if (Click != null) {
Click(this, e);
}
}
protected override void RenderContents( HtmlTextWriter output)
{
output.WriteBeg inTag("div");
output.WriteAtt ribute("style", "cursor:pointer ;");
output.WriteAtt ribute("onclick ", "alert('test'); ");
output.Write(Ht mlTextWriter.Ta gRightChar);
output.Write(Te xt);
output.WriteEnd Tag("div");
}
}