[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

<asp:Label> Click event handler !

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
0
milani_lucie
Asked:
milani_lucie
  • 3
  • 2
1 Solution
 
sunithnairCommented:
You mean you want to add server side event or client side using javascript?
0
 
milani_lucieAuthor Commented:
I want to add server side event handler.

Thanks
0
 
sunithnairCommented:
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
milani_lucieAuthor 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
0
 
sunithnairCommented:
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

0
 
havj123Commented:
Why do u want to click a label? If u want to raise a click event then use a button or link button.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now