Friday 11 May 2012

Checkbox

Checkbox is used to select multiple items including zero, one or more at a same time .In other words we can say each checkbox is independent to other checkboxes so checking one checkbox does not matter the other unchecked boxes.To add a checkbox in your project goto toolbox and double click on Checkbox control.

          <asp:CheckBox ID="CheckBox1" runat="server" Text="Checkbox" Checked="true"/>

By using checked property is true you can make checkbox initially checked and by specifying false you can make checkbox initially unchecked.By using text property you can display text with your checkbox as given below.



Some Useful Properties of Checkbox:


ID Property:
Checkbox.ID property id used to set the ID of checkbox control.


protected void Page_Load(object sender, System.EventArgs e)
{
   // Set a ID to a Checkbox
   checkbox.ID = "chkUserName";
}



Text Property:
We can set a value to checkbox by using  Checkbox1.Text  property.For example


protected void Page_Load(object sender, System.EventArgs e)
{
   // set a text to checkbox
    Checkbox1.Text = "Remeber me";
}

Enabled Property:
Checkbox.Enabled property enable the state of  Checkbox control..Initially its value is true.

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

Visible Property:


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




protected void Button1_Click(object sender, System.EventArgs e)
{
   checkbox1.Visible = 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)
{
    chekbox1.Height = 100px;
    chekbox1.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="checkbox1" runat="server" Text="Subjects"  CssClass="HomePage"></asp:Label>



Some Useful Events of Checkbox


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 Checkbox1_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 Checkbox_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  ?

Checkbox1.Focus() method is use to set the control on a Checkbox.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 Checkbox_Load(object sender, EventArgs e)
{
  Checkbox1.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:Checkbox ID="Checkbox1" runat="server"></asp: Checkbox >
      <br />
      <asp:Button ID="Button1" runat="server" Text="Button" />
      <br />
    </div>
  </form>
</body>
</html>


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


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




To see more properties of checkbox click the link given below:


http://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox_properties.aspx

No comments:

Post a Comment