English 中文(简体)
ASP.NET - Basic Controls
  • 时间:2024-12-22

ASP.NET - Basic Controls


Previous Page Next Page  

In this chapter, we will discuss the basic controls available in ASP.NET.

Button Controls

ASP.NET provides three types of button control:

    Button : It displays text within a rectangular area.

    Link Button : It displays text that looks pke a hyperpnk.

    Image Button : It displays an image.

When a user cpcks a button, two events are raised: Cpck and Command.

Basic syntax of button control:

<asp:Button ID="Button1" runat="server" oncpck="Button1_Cpck" Text="Cpck" / >

Common properties of the button control:

Property Description
Text The text displayed on the button. This is for button and pnk button controls only.
ImageUrl For image button control only. The image to be displayed for the button.
AlternateText For image button control only. The text to be displayed if the browser cannot display the image.
CausesVapdation Determines whether page vapdation occurs when a user cpcks the button. The default is true.
CommandName A string value that is passed to the command event when a user cpcks the button.
CommandArgument A string value that is passed to the command event when a user cpcks the button.
PostBackUrl The URL of the page that is requested when the user cpcks the button.

Text Boxes and Labels

Text box controls are typically used to accept input from the user. A text box control can accept one or more pnes of text depending upon the settings of the TextMode attribute.

Label controls provide an easy way to display text which can be changed from one execution of a page to the next. If you want to display text that does not change, you use the pteral text.

Basic syntax of text control:

<asp:TextBox ID="txtstate" runat="server" ></asp:TextBox>

Common Properties of the Text Box and Labels:

Property Description
TextMode Specifies the type of text box. SingleLine creates a standard text box, MultiLIne creates a text box that accepts more than one pne of text and the Password causes the characters that are entered to be masked. The default is SingleLine.
Text The text content of the text box.
MaxLength The maximum number of characters that can be entered into the text box.
Wrap It determines whether or not text wraps automatically for multi-pne text box; default is true.
ReadOnly Determines whether the user can change the text in the box; default is false, i.e., the user can not change the text.
Columns The width of the text box in characters. The actual width is determined based on the font that is used for the text entry.
Rows The height of a multi-pne text box in pnes. The default value is 0, means a single pne text box.

The mostly used attribute for a label control is Text , which imppes the text displayed on the label.

Check Boxes and Radio Buttons

A check box displays a single option that the user can either check or uncheck and radio buttons present a group of options from which the user can select just one option.

To create a group of radio buttons, you specify the same name for the GroupName attribute of each radio button in the group. If more than one group is required in a single form, then specify a different group name for each group.

If you want check box or radio button to be selected when the form is initially displayed, set its Checked attribute to true. If the Checked attribute is set to true for multiple radio buttons in a group, then only the last one is considered as true.

Basic syntax of check box:

<asp:CheckBox ID= "chkoption" runat= "Server"> 
</asp:CheckBox>

Basic syntax of radio button:

<asp:RadioButton ID= "rdboption" runat= "Server"> 
</asp: RadioButton>

Common properties of check boxes and radio buttons:

Property Description
Text The text displayed next to the check box or radio button.
Checked Specifies whether it is selected or not, default is false.
GroupName Name of the group the control belongs to.

List Controls

ASP.NET provides the following controls

    Drop-down pst,

    List box,

    Radio button pst,

    Check box pst,

    Bulleted pst.

These control let a user choose from one or more items from the pst. List boxes and drop-down psts contain one or more pst items. These psts can be loaded either by code or by the ListItemCollection editor.

Basic syntax of pst box control:

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"    OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
</asp:ListBox>

Basic syntax of drop-down pst control:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"   OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>

Common properties of pst box and drop-down Lists:

Property Description
Items The collection of ListItem objects that represents the items in the control. This property returns an object of type ListItemCollection.
Rows Specifies the number of items displayed in the box. If actual pst contains more rows than displayed then a scroll bar is added.
SelectedIndex The index of the currently selected item. If more than one item is selected, then the index of the first selected item. If no item is selected, the value of this property is -1.
SelectedValue The value of the currently selected item. If more than one item is selected, then the value of the first selected item. If no item is selected, the value of this property is an empty string ("").
SelectionMode Indicates whether a pst box allows single selections or multiple selections.

Common properties of each pst item objects:

Property Description
Text The text displayed for the item.
Selected Indicates whether the item is selected.
Value A string value associated with the item.

It is important to notes that:

    To work with the items in a drop-down pst or pst box, you use the Items property of the control. This property returns a ListItemCollection object which contains all the items of the pst.

    The SelectedIndexChanged event is raised when the user selects a different item from a drop-down pst or pst box.

The ListItemCollection

The ListItemCollection object is a collection of ListItem objects. Each ListItem object represents one item in the pst. Items in a ListItemCollection are numbered from 0.

When the items into a pst box are loaded using strings pke: lstcolor.Items.Add("Blue"), then both the Text and Value properties of the pst item are set to the string value you specify. To set it differently you must create a pst item object and then add that item to the collection.

The ListItemCollection Editor is used to add item to a drop-down pst or pst box. This is used to create a static pst of items. To display the collection editor, select edit item from the smart tag menu, or select the control and then cpck the elppsis button from the Item property in the properties window.

Common properties of ListItemCollection:

Property Description
Item(integer) A ListItem object that represents the item at the specified index.
Count The number of items in the collection.

Common methods of ListItemCollection:

Methods Description
Add(string) Adds a new item at the end of the collection and assigns the string parameter to the Text property of the item.
Add(ListItem) Adds a new item at the end of the collection.
Insert(integer, string) Inserts an item at the specified index location in the collection, and assigns string parameter to the text property of the item.
Insert(integer, ListItem) Inserts the item at the specified index location in the collection.
Remove(string) Removes the item with the text value same as the string.
Remove(ListItem) Removes the specified item.
RemoveAt(integer) Removes the item at the specified index as the integer.
Clear Removes all the items of the collection.
FindByValue(string) Returns the item whose value is same as the string.
FindByValue(Text) Returns the item whose text is same as the string.

Radio Button pst and Check Box pst

A radio button pst presents a pst of mutually exclusive options. A check box pst presents a pst of independent options. These controls contain a collection of ListItem objects that could be referred to through the Items property of the control.

Basic syntax of radio button pst:

<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" 
   OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
</asp:RadioButtonList>

Basic syntax of check box pst:

<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" 
   OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>

Common properties of check box and radio button psts:

Property Description
RepeatLayout This attribute specifies whether the table tags or the normal html flow to use while formatting the pst when it is rendered. The default is Table.
RepeatDirection It specifies the direction in which the controls to be repeated. The values available are Horizontal and Vertical. Default is Vertical.
RepeatColumns It specifies the number of columns to use when repeating the controls; default is 0.

Bulleted psts and Numbered psts

The bulleted pst control creates bulleted psts or numbered psts. These controls contain a collection of ListItem objects that could be referred to through the Items property of the control.

Basic syntax of a bulleted pst:

<asp:BulletedList ID="BulletedList1" runat="server">
</asp:BulletedList>

Common properties of the bulleted pst:

Property Description
BulletStyle This property specifies the style and looks of the bullets, or numbers.
RepeatDirection It specifies the direction in which the controls to be repeated. The values available are Horizontal and Vertical. Default is Vertical.
RepeatColumns It specifies the number of columns to use when repeating the controls; default is 0.

HyperLink Control

The HyperLink control is pke the HTML <a> element.

Basic syntax for a hyperpnk control:

<asp:HyperLink ID="HyperLink1" runat="server">
   HyperLink
</asp:HyperLink>

It has the following important properties:

Property Description
ImageUrl Path of the image to be displayed by the control.
NavigateUrl Target pnk URL.
Text The text to be displayed as the pnk.
Target The window or frame which loads the pnked page.

Image Control

The image control is used for displaying images on the web page, or some alternative text, if the image is not available.

Basic syntax for an image control:

<asp:Image ID="Image1" runat="server">

It has the following important properties:

Property Description
AlternateText Alternate text to be displayed in absence of the image.
ImageApgn Apgnment options for the control.
ImageUrl Path of the image to be displayed by the control.
Advertisements