We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

<asp:Label> Click event handler !

milani_lucie
milani_lucie asked
on
Medium Priority
745 Views
Last Modified: 2012-05-06
Hi,

I have a ASP.NET label control. I want to have a click event on this. By default it is not having any "Click" event. Can this be done by using delegates ? Can you please provide me sample code for this !

Thanks
Comment
Watch Question

You mean you want to add server side event or client side using javascript?

Author

Commented:
I want to add server side event handler.

Thanks
If you want in javascript then you can do it like this
CodeBehind
 
lbl.Attributes.Add("onclick", "JFunction(this)");
 
JavaScript
<script language='javascript'>
function JFunction(src)
{
//do whatever you want with the object src
}
</script>

Open in new window

Author

Commented:
Here is what i have got from the web:
==============================

Derive from the existing labelcontrol to create your control and implement a ipostback handler and expose a event on label click. Lable is span in html so just check outwith the span clientside event to
which you want to map the postback.

Thanks
You cant directly add onclick for a label as it just renders as a <span></span> tag when generated. You can have a workaround though
ASPX
 
<script language='javascript'>
  function JFunction(src)
  {
    var btn = document.getElementById("<%=hdnButton.ClientID%>");
    document.getElementById("<%=hdnSrc.ClientID%>").value = src.id;
    btn.click();
  }
</script>
<asp:Label ID="lbl" runat="server" />
<asp:HiddenField ID="hdnSrc" runat="server" />
<asp:Button id="hdnButton" runat="server" onclick="hdnButton_Click" />
 
CODE BEHIND
 
Page_Load
{
if(!IsPostBack)
{
hdnButton.Attributes.Add("style", "display:none");
lbl.Attributes.Add("onclick", "JFunction(this)");
}
}
 
hdnButton_Click
{
//you will get the id of the label clicked from hdnSrc.Value
//do the code required
}
Code Behind

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Why do u want to click a label? If u want to raise a click event then use a button or link button.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.