473,785 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom WebControl with OnClick event

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");
}
}
Oct 3 '07 #1
3 5710
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 IPostBackEventH andler, or directly from the button
class.
Otherwise, you have to handle the whole thing with Javascript.
The GetPostBackRefe rence Methode gives you a return value, that you must bin
to a hyperlink target or an clientside eventhandler of an Html Control
(<Control>.Attr ibutes.Add(<att ribute>, <postback reference>)).

Such a control have to implement the IPostBackEventH andler 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/lib...on(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/lib...er(VS.80).aspx

The GetPostBackRefe renceMethode can found in the Page.ClientScri pt class:
-
http://msdn2.microsoft.com/en-us/lib...pt(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
Oct 3 '07 #2
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
Oct 3 '07 #3
Thank you, I got it working.
I did not expect it to that trivial.

Tomasz J
Oct 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2026
by: Jim Mitchell | last post by:
I added a toolbar and then added two images that the user can not click on. I double click on the toolbar in design mode and it does not seem to create any kind of OnClick Event. Can someone show me some sample code for the Toolbar's onclick event? Thanks in advance.
1
1670
by: chris | last post by:
Hi there, I would like to add an onClick event to a webcontrol which calls a function which is in the code behind an not a javascript. Would that be possible? Something like this: MenuCase.Attributes.Add("onClick", MenuCase_ButtonClick()); private string MenuCase_ButtonClick()
3
7731
by: Arulraja | last post by:
Hello, I have created 2 custom server controls, The parent custom control contains multiple child custom controls. The Child control has a button on it. If I Click the button control, it calls the Parent controls CreateChildControls() method before calling the child control's button click event. Because of this behaviour, I am not getting the changes I made in the
2
5930
by: Novice | last post by:
I can't seem to get my Custom WebControl to output a button whose click event is associated with a particular method. Here is the code I have right now that contains a panel and there is a button in that panel that should be associated with a event method - but it isn't working: protected override void Render(HtmlTextWriter output) { Panel panel = new Panel(); nextButton = new Button();
0
1818
by: Novice | last post by:
Hey all, I've already posted this question and two posters offered suggestions and they worked - but now I would like to know why - and if possible the answer to a second question. Here is my problem: I am trying to write a custom WebControl - at some point this WebControl will output a bunch of controls into several Panel objects. But I just recently got my WebControl to output a single button and attach an event handler to that...
3
5168
by: RKT | last post by:
(CSharp, NET2.0) Is there any way to EITHER: 1. Programmatically add a server onclick() event hander to the 'HtmlImage' HtmlControl control? OR 2. Programmatically preempt a 'Image' WebControl from invoking a postback?
0
897
by: Ernest Morariu | last post by:
Hi All ! aspnet 2005; C# I created a control deriving from System.Web.UI.WebControls.ImageButton. Besides the control itself the project contains also some aspx pages. The onclick event of this control is executed on client side(JavaScript). The code in the onclick, opens a new window with showModalDialog() calling one of the aspx pages inside my project.
2
2543
by: Peter Rilling | last post by:
Okay, I something weird is happening where I do not know if I am doing something wrong. I have a page that contains a custom webcontrol that I developed. This webcontrol basically loads a UserControl (ascx) using the LoadControl method and writing it out to the browser. Page --> custom webcontrol --> usercontrol (via LoadControl) --> stream to browser.
5
13963
by: Stuart Shay | last post by:
Hello All I am working on ASP.NET 1.1 Custom Pager that allows a User to Enter a Number in a TextBox and go to the page selected. Since the OnClick Event does not work in ASP.NET 1.1 for a TextBox I want to use a hidden button to fire when the Onclick Event is fired for the TextBox.
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.