jQuery and ASP.NET

April 3, 2009

Adding OnClick event to an ASP.NET Label control




It's a strange requirement, but I bumped into it when helping out a user on the forums. Here's how to add an OnClick event to the ASP.NET Label Control


 


<html xmlns="http://www.w3.org/1999/xhtml">


<head runat="server">


    <title></title>


    <script type="text/javascript">


        function CallMe() {


            alert('Hi');


        }


    </script>


</head>


<body>


    <form id="form1" runat="server">


    <div>


        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


    </div>


    </form>


</body>


</html>




Now in the Code Behind, use the Control.Attributes.Add() to add the OnClick event to the Label Control

C#


    protected void Page_Load(object sender, EventArgs e)


    {


        Label1.Attributes.Add("onClick", "CallMe();");


    }




VB.NET


 


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)


    Label1.Attributes.Add("onClick", "CallMe();")


End Sub




'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

2 Responses to "Adding OnClick event to an ASP.NET Label control"
  1. JesR said...
    April 6, 2009 8:32 PM

    You saved me a couple of hours!

  2. Anonymous said...
    April 6, 2011 4:53 AM

    can nayone tell me how to play songs on click event in asp.net application .i have ten mp3 songs which i have to play on the click event.plase help me.
    send me reply on siddhu.chauhan30@gmail.com

 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions