Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to add an onclick event to an ASP.NET Label server control?

I wanted to do something like this:

<asp:Label ID="lblMyLabel" onclick="lblMyLabel_Click" runat="server">My Label</asp:Label> 

I know that in Javascript I can do:

<span onclick="foo();">My Label</span> 

So I'm wondering why I can't do that with a Label object.

like image 801
fuentesjr Avatar asked Oct 08 '08 01:10

fuentesjr


People also ask

How to add the onclick event in Label ASP net?

You can use Attributes to add onclick client side callback. I didn't know you can do this on span tags, but if it works you can add 'onclick' by lblMyLabel. Attributes. Add("onclick", "foo();");

Can we use Onclick in label in HTML?

Both radio and checkbox HTML type s can have an onclick call out to JavaScript.

What is onclick event in asp net?

The Click event is raised when the Button control is clicked. This event is commonly used when no command name is associated with the Button control (for instance, with a Submit button). Raising an event invokes the event handler through a delegate.


1 Answers

You can use Attributes to add onclick client side callback.

I didn't know you can do this on span tags, but if it works you can add 'onclick' by lblMyLabel.Attributes.Add("onclick", "foo();");

But foo(); would need to be a client side javascript function.

System.Web.UI.WebControls.Label does NOT have OnClick server event. You could look into using AJAX if you want server callback with example above.

You could also use LinkButton like other say. You can make it not look like a link by using CSS, if it is just that underline you are concerned about.

ASPX:

<asp:LinkButton ID="LinkButton1" runat="server"      CssClass="imjusttext" OnClick="LinkButton1_Click"> LinkButton </asp:LinkButton> 

CSS:

a.imjusttext{ color: #000000; text-decoration: none; } a.imjusttext:hover { text-decoration: none; } 
like image 105
Brian Kim Avatar answered Sep 16 '22 16:09

Brian Kim