Onlineatoz.net | ToolBox Standard Control Checkbox

ToolBox Standard Control Checkbox

Checkbox and CheckboxList Web Server Control

Commonly,The CheckBox control can be used to performing check or uncheck that means yes/no operations. here, ASP.NET provides CheckBoxList Web server controls differ from the HTML Elements. there Checkbox and CheckBoxList controls are two different controls, and contains different function.

CheckBox Control - CheckBoxList Control Difference

CheckBox controls to a page is single check box control. Alternatively, the CheckBoxList control is a single control that acts as a parent control for a collection of check-box list items. It derives from a base ListControl class, and therefore works much like the ListBox, DropDownList, RadioButtonList, and BulletedList Web server controls. For that reason, many of the procedures for working with the CheckBoxList control are the same as the procedures for the other list Web server controls.

CheckboxList can mostly be used when needs to create more than one checkboxes and bind data from database.

CheckBox Control HTML Attribute

When the CheckBox control renders to the browser, it contains two parts: an input element representing the check box, and a separate label element representing the caption for the check box. The combination of the two elements is in turn wrapped in a span element.

Add Items in List

When you add a list item, you must specify the three properties of the item as,

  • Text - The text displayed in the list.
  • Value -The value associated with an item. Setting this property allows you to associate a value with a specific item without displaying it.
  • Selected -A Boolean value indicating whether the item is selected.
To add items at design time

When you add a list item, you must specify the three properties of the item as,

  • Type an <asp:ListItem> element in the list control.
  • Set the Text and Value properties of the new list item.
  • Optionally, set the Selected property for one items.

the following code snippets demonstrates a listbox control

<asp:ListBox ID="ListBox1" runat="server">
    <asp:ListItem Text="Apple" Value="App" Selected="True" />
    <asp:ListItem Text="Orange" Value="Ora" />
    <asp:ListItem Text="Banana" Value="Ban" />
</asp:ListBox>
                 
 
To add items programmatically
  • Create a new object of type ListItem and set its Text and Value properties.
  • Call the Add method of the control's Items collection and pass it the new object.

the following code snippets demonstrates this

Protected void Button1_Click (object sender, System.EventArgs e)
{
    ListBox1.Items.Add(new ListItem("Apple", "App"));
    ListBox1.Items.Add(new ListItem("Orange", "Ora"));
    ListBox1.Items.Add(new ListItem("Banana", "Ban"));
}
                 
 
To add items programmatically via Data Source
  • set the control's AppendDataBoundItems property to true.
  • Add a data source control to the page, such as a SqlDataSource or ObjectDataSource control. For details, see Data Source Controls Overview.
  • Configure the data source control with connection string and query information.
  • Set the list control's DataSourceID property to the ID of the data source control.
  • Set the Properties such as DataTextField and DataTextFormatString

the following code snippets demonstrates this

<asp:ListBox ID="ListBox1" runat="server" 
  DataSourceID="SqlDataSource1" 
  DataTextField="CategoryName" 
  DataValueField="CategoryID">
</asp:ListBox>

<asp:SqlDataSource 
   ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    ID="SqlDataSource1" 
   runat="server" 
   SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
</asp:SqlDataSource>
                 
 
Related Links

Posted by: Admin
Posted on: 9/13/2011 at 7:45 PM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed