How to add an onClick event to a Label in

Add an onClick event to a Label
The Label class Represents a label control, which displays text on a Web page. The developers can use the Label control to display text in a set location on the web page. Unlike static text, c# developers can customize the displayed text through the Text property.

The following c# tutorial code demonstrates how we can add an onClick event to a Label web server control. But there is no built-in onClick event in the Label control. So we have to find an alternate solution to attach a click event to a Label control.

In the below c# code, we used the Label control’s Attributes property to add a click event to a Label web server control. The Attributes property allows us to add a javascript click event to a Label server control.

The Label Attributes property gets the collection of arbitrary attributes (for rendering only) that do not correspond to properties on the control. This property is inherited from WebControl. The Attributes property value is an AttributeCollection of name and value pairs.

So finally, using this Attributes property the developers can add a javascript onClick event to the Label control. In this example code, we showed an alert on the web browser when someone clicks the Label control.

<%@ Page Language="C#" AutoEventWireup="true"%>  
<!DOCTYPE html>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
        Label1.Attributes.Add("OnClick", "javascript:alert('label clicked.')");
<html xmlns="">      
<head id="Head1" runat="server">      
    <title> example - label onclick</title>      
    <form id="form1" runat="server">      
        <h2 style="color:MidnightBlue; font-style:italic;">      
   example - label onclick
        <hr width="550" align="left" color="Gainsboro" />      
            Text="Click Here To Test Label OnClick Event."    
More examples