PC Review


Reply
Thread Tools Rate Thread

Custom WebControl with OnClick event

 
 
Tomasz J
Guest
Posts: n/a
 
      3rd Oct 2007
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");
}
}


 
Reply With Quote
 
 
 
 
Peter Bucher [MVP]
Guest
Posts: n/a
 
      3rd Oct 2007
Hallo TomaszJ

> How do create a custom WebControl (not UserControl), exposing OnClick
> event?
> I reviewed countless examples found by Google, but came across nothing
> helpful.

Any Control beside the Button itself uses clientside Javascript to do a
PostBack and raise Events.
If you want to create a Button like control that have all the
functionalities like CommandName and CommandArgument, you have to derive
from IButtonControl And IPostBackEventHandler, or directly from the button
class.
Otherwise, you have to handle the whole thing with Javascript.
The GetPostBackReference Methode gives you a return value, that you must bin
to a hyperlink target or an clientside eventhandler of an Html Control
(<Control>.Attributes.Add(<attribute>, <postback reference>)).

Such a control have to implement the IPostBackEventHandler interface to
raise the event.

A good example of an control that handle an event that raised by javascript
is the Link Button:
-
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton(VS.80).aspx

An example for an very, very basic button control as customcontrol can be
found here (only in german):
-
http://msdn2.microsoft.com/de-de/library/system.web.ui.ipostbackeventhandler(VS.80).aspx

The GetPostBackReferenceMethode can found in the Page.ClientScript class:
-
http://msdn2.microsoft.com/en-us/library/system.web.ui.page.clientscript(VS.80).aspx


HTH

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET


 
Reply With Quote
 
Peter Bucher [MVP]
Guest
Posts: n/a
 
      3rd Oct 2007
Hi Tomasz again

I have forgot a detail ;-)
You can view the code from the LinkButton, or any other control with .NET
Reflector.
- http://www.aisto.com/roeder/dotnet/

--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET


 
Reply With Quote
 
Tomasz J
Guest
Posts: n/a
 
      3rd Oct 2007
Thank you, I got it working.
I did not expect it to that trivial.

Tomasz J


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Event on Custom WebControl Alhambra Eidos Kiquenet Microsoft ASP .NET 1 27th Nov 2007 02:42 PM
onClick event for webcontrol =?Utf-8?B?Y2hyaXM=?= Microsoft C# .NET 1 31st Mar 2005 08:02 AM
Event Handler for button click in Custom WebControl =?Utf-8?B?Tm92aWNl?= Microsoft ASP .NET 0 24th Sep 2004 02:19 PM
Button in custom webcontrol - event handler not called =?Utf-8?B?RGl2eWE=?= Microsoft ASP .NET 1 1st May 2004 05:35 AM
OnClick Event for Toolbar WebControl Jim Mitchell Microsoft Dot NET 1 12th Jul 2003 02:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:05 AM.