The Label control's AssociatedControlID property-ASP.NET

When you add a Label control to your page, you can associate the label with control, such as a TextBox or CheckBtc. Suppose you use the AssociatedControlID and associate a control to the label. In that case, the runtime will automatically render the “for” attribute to the label element (The “for” attribute is used to specify which control the label is associated with).

For example:

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

The control above will be generated to the following code at runtime:

 <label for="TextBox1" id="Label2">
                Label</label>
            <input name="TextBox1" type="text" id="Text1" />

If the “for” attribute is added to a label element, you can click the mouse button on the label. The associated control (in the above example, the TextBox) will be selected. If you associate a checkbox to a label, the checkbox will be selected or deselected if you click on the label associated with the checkbox.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LabelClik.aspx.cs" Inherits="LabelClik" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:Label ID="Label1" runat="server" Text="Label" AssociatedControlID="TextBox1">></asp:Label>
          <asp:TextBox ID="TextBox1" runat="server">
      </div>
  </form>
</body>
</html>

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post