T
Tomasz J
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.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyButton runat=\"server\"></{0}:MyCloseButton>")]
public class MyButton : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(false)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
// should I use: Attributes.Add("OnClick",
Page.Clientscript.GetPostBackEventReference(this, ID.ToString))
[Category("New"), Browsable(true)]
public event EventHandler Click;
protected void OnClick(EventArgs e)
{
if (Click != null) {
Click(this, e);
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.WriteBeginTag("div");
output.WriteAttribute("style", "cursorointer;");
output.WriteAttribute("onclick", "alert('test');");
output.Write(HtmlTextWriter.TagRightChar);
output.Write(Text);
output.WriteEndTag("div");
}
}
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.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyButton runat=\"server\"></{0}:MyCloseButton>")]
public class MyButton : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(false)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
// should I use: Attributes.Add("OnClick",
Page.Clientscript.GetPostBackEventReference(this, ID.ToString))
[Category("New"), Browsable(true)]
public event EventHandler Click;
protected void OnClick(EventArgs e)
{
if (Click != null) {
Click(this, e);
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.WriteBeginTag("div");
output.WriteAttribute("style", "cursorointer;");
output.WriteAttribute("onclick", "alert('test');");
output.Write(HtmlTextWriter.TagRightChar);
output.Write(Text);
output.WriteEndTag("div");
}
}