implementing label onClick

nbogdanovic

Member
Joined
Sep 26, 2006
Messages
5
Location
Washington, DC
Programming Experience
3-5
Hi,

I am trying to execute a piece of code if somebody clicks on a label. Does anybody know if that is possible since labels don't have an onClick event?

Anyhow, I am trying to create a user object that consist of a button, and there are some labels on that button. I want to make sure that no matter where in the button a user clicks, button.onClick method will be executed. But if I click on the label area nothing happens, and that is where my problem is coming from.

Thanks.
 
Almost all Html elements have 'onclick' event for DHtml client scripting, so you can add this attribute to the generated client Html code. If I understand you want to make the server-side button event code trigger if button or label is clicked, here is example of how to do this, in Javascript also the label onclick performs the button click:
VB.NET:
[SIZE=2][COLOR=#0000ff]Protected [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Page_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _[/SIZE]
[SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2]  Label1.Attributes.Add([/SIZE][SIZE=2][COLOR=#800000]"onclick"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"javascript:"[/COLOR][/SIZE][SIZE=2] & Button1.ClientID & [/SIZE][SIZE=2][COLOR=#800000]".click()"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Protected [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _[/SIZE]
[SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]  Me[/COLOR][/SIZE][SIZE=2].Title = [/SIZE][SIZE=2][COLOR=#800000]"label1 or button1 clicked"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
Thanks JohnH

Thank you,

that is exactly what I was looking for. Although now I am wondering how I could get the default behavior on a button click. If I just click on the label it calls the button click event, but it does not give me the impression that the button was actually clicked. Do you have any ideas what I need to call in order for this to work.

I assume that these are all very silly questions, but I am just starting with VB.NET so I need a lot of help and I found this website to be one of the best.

Thanks.
 
You might be able to do it with images for the different states changing with events onmouseover and onmousedown/onmouseup.
 
Back
Top