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.
you could always roll out your own control which produces a span, with the .net's standard postback javascript, but as stated earlier using a linklabel with a CSS class would be easier
I think you can, but it's a client-side onclick handler, not server side. It will complain about the attribute not being supported (or some such) but I think it renders correctly. If you want to to a server-side handler, I think you'll need to do a LinkButton.
As far as I know that's impossible. Label control emits
<span
> element which is “unclickable” on the server side. You would need to replace your Label control with a LinkButton.You need to create a class object that inherits from the label and onclick event handler which will be a method by yourslef and use your object as a custom label.
If you want an onclick event, why don't you use a linkbutton, which has the
onclientclick
event:You can use CSS to make the link look like whatever you want
Another hack would be to use a hidden link or button with style="display:none;" and trigger the click on the server control from a javascript function in the span.
Something like this: