Monday 7 May 2012

Labels

In asp.net labels are used to display messages for a user to enter a correct input.To include a labels in your project goto toolbox  and double click on Labels.Following text will be appeared.


<%@ Page Language="C#" %>
<html>
<head runat="server">
  <title>Test Page</title>
</head>
<body>
  <form id="form1" runat="server" defaultfocus="TextBox1" >
    <div>
          <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
  </form>
</body>
</html>
Some Useful Properties of Label:

ID Property:
Label1.ID property id used to set the ID of label control.



protected void Page_Load(object sender, System.EventArgs e)
{
   // Set a ID to a Label
   Label1.ID = "lblUserName";
}


Text Property:
We can get value from a label by using Label1.Text property and set a value to textbox by using  Label1.Text  property.For example

protected void Page_Load(object sender, System.EventArgs e)
{
   // Get a value from Label
   string userName = Label1.Text;
   //Set a value to Label
    Label1.Text = userName;
}

protected void Button1_Click(object sender, System.EventArgs e)
{
     Label1.Text =  Label2.Text;
}

Enabled Property:
Label.Enabled property anable the state of  Label control.It is similar to Readonly property   but the difference is that you can copy the contents by Readonly property but with Enabled    property you even can't copy the contents.Initially its value is true.

protected void Button1_Click(object sender, System.EventArgs e)
{
    Label1.Text = false;
}

Visible Property:

Textbox.Visible property is used to set the visiblity of a textbox.If its value is true then textbox will be visible otherwise it will be hide for user.



protected void Button1_Click(object sender, System.EventArgs e)
{
   TextBox1.Text = false;
}

Height & Width Property:
You can use the Height property along with the Width property  to size an object to specific  dimensions.



protected void Button1_Click(object sender, System.EventArgs e)
{
   Label1.Height = 100px;
   Labael1.Width = 100px;
}


CssClass Property:
You can use the CssClass property to specify the CSS(Cascadding Style Sheet) class to render on the client for the Web Server control. This property will render on browsers for all controls. In simple words you can add CssClass by using CssClass property by just giving a cssclass name.



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


Some Useful Events of Label


Init Event:
This event fires when the page has been initialized.During page initialization, controls on the page are accessible and each control's UniqueID property is set. A master page and themes are also applied to the page if required. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.


protected void Label1_Init(object sender, EventArgs e)
{
   Here you can write your logic...
}

Load Event:

This event fires when the web page has been loaded.During load, the opposite happens to the Init Event, all the postback data has been set and all the controls are ready and can fire their different events. The top container, the content page loads first as it can change the master page and user controls, then the master page and in the end the leaf controls.

protected void Label_Load(object sender, System.EventArgs e)
{
   Here You can write your code.
}

Some Useful Methods of Label

Focus Method:

How to Set Focus on Asp.Net  ?

Label1.Focus() method is use to set the control on a Label.Focus method is helpful to reduce the use of mouse and user can easily get a control by using keyboard.


AT Server Side : 

protected void Page_Load(object sender, EventArgs e)
{
  Label1.Focus();
}


At Client Side:

<%@ Page Language="C#" %>
<html>
<head runat="server">
  <title>Test Page</title>
</head>
<body>
  <form id="form1" runat="server" defaultfocus="TextBox1" >
    <div>
      <asp:Label ID="Label1" runat="server"></asp:Label>
      <br />
      <asp:Button ID="Button1" runat="server" Text="Button" />
      <br />
    </div>
  </form>
</body>
</html>


Databind() Method:
Label.Databind() binds the datasource to the invoked server control and all its child controls.


Dispose() Method:
Label.Dispose()  enables a server control to perform a final cleanup before it is released from  memory.

To see more properties about Label click on following link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.label.aspx



No comments:

Post a Comment