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="">

<head runat="server">


    <script type="text/javascript">

        function CallMe() {






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


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





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


    protected void Page_Load(object sender, EventArgs e)


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




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

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

End Sub

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) 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 is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal


JesR said...

You saved me a couple of hours!

Anonymous said...

can nayone tell me how to play songs on click event in application .i have ten mp3 songs which i have to play on the click event.plase help me.
send me reply on

Anonymous said...

Could you please explain how to catch the event? I just get a java error now and adding a sub in does not work (adding a sub CallMe).