在本章中,我們將討論ASP.NET中可用的基本控制項。
Button Controls
ASP.NET提供三種類型的按鈕控制項:
按鈕:它在矩形區域內顯示文本。
它顯示創建爲超連結的文本。
圖像按鈕:顯示圖像。
當用戶單擊一個按鈕時,會引發兩個事件:Click和Command。
按鈕控制項的基本語法:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Click" / >
按鈕控制項的公共屬性:
Property | Description |
---|---|
Text | The text displayed on the button. This is for button and link 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. |
CausesValidation | Determines whether page validation occurs when a user clicks the button. The default is true. |
CommandName | A string value that is passed to the command event when a user clicks the button. |
CommandArgument | A string value that is passed to the command event when a user clicks the button. |
PostBackUrl | The URL of the page that is requested when the user clicks the button. |
Text Boxes and Labels
文本框控制項通常用於接受用戶的輸入。文本框控制項可以接受一行或多行文本,具體取決於TextMode屬性的設置。
標籤控制項提供了一種簡單的顯示文本的方法,文本可以從頁面的一次執行更改爲下一次執行。如果要顯示不更改的文本,請使用文本。
文本控制項的基本語法:
<asp:TextBox ID="txtstate" runat="server" ></asp:TextBox>
文本框和標籤的常見屬性:
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 line 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-line 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-line text box in lines. The default value is 0, means a single line text box. |
label控制項最常用的屬性是「Text」,它表示標籤上顯示的文本。
Check Boxes and Radio Buttons
複選框顯示用戶可以選中或取消選中的單個選項,單選按鈕顯示一組選項,用戶可以從中僅選擇一個選項。
要創建一組單選按鈕,請爲組中每個單選按鈕的group name屬性指定相同的名稱。如果一個表單中需要多個組,則爲每個組指定不同的組名。
如果希望在最初顯示窗體時選中複選框或單選按鈕,請將其選中屬性設置爲true。如果一個組中的多個單選按鈕的Checked屬性設置爲true,則只有最後一個被視爲true。
複選框的基本語法:
<asp:CheckBox ID= "chkoption" runat= "Server"> </asp:CheckBox>
單選按鈕的基本語法:
<asp:RadioButton ID= "rdboption" runat= "Server"> </asp: RadioButton>
複選框和單選按鈕的常見屬性:
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提供以下控制項
- Drop-down list,
- List box,
- Radio button list,
- Check box list,
- Bulleted list.
這些控制項允許用戶從列表中選擇一個或多個項。列表框和下拉列表包含一個或多個列表項。這些列表可以通過代碼或ListItemCollection編輯器加載。
列表框控制項的基本語法:
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"> </asp:ListBox>
下拉列表控制項的基本語法:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList>
列表框和下拉列表的常見屬性:
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 list 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 list box allows single selections or multiple selections. |
每個列表項對象的公共屬性:
Property | Description |
---|---|
Text | The text displayed for the item. |
Selected | Indicates whether the item is selected. |
Value | A string value associated with the item. |
需要注意的是:
若要使用下拉列表或列表框中的項,請使用控制項的「項」屬性。此屬性返回包含列表中所有項的ListItemCollection對象。
當用戶從下拉列表或列表框中選擇其他項時,將引發SelectedIndexChanged事件。
The ListItemCollection
ListItem collection對象是ListItem對象的集合。每個list item對象表示列表中的一個項。ListItemCollection中的項從0開始編號。
當使用諸如:lstcolor.items.Add(「Blue」)之類的字符串將項加載到列表框中時,列表項的文本和值屬性都將設置爲指定的字符串值。若要進行不同的設置,必須創建列表項對象,然後將該項添加到集合中。
ListItemCollection編輯器用於將項添加到下拉列表或列表框中。這用於創建項目的靜態列表。若要顯示集合編輯器,請從智能標記菜單中選擇「編輯項目」,或選擇控制項,然後在「屬性」窗口中的「項目」屬性中單擊省略號按鈕。
ListItemCollection的公共屬性:
Property | Description |
---|---|
Item(integer) | A ListItem object that represents the item at the specified index. |
Count | The number of items in the collection. |
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 list and Check Box list
單選按鈕列表顯示互斥選項的列表。複選框列表顯示獨立選項的列表。這些控制項包含可以通過控制項的Items屬性引用的ListItem對象的集合。
單選按鈕列表的基本語法:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"> </asp:RadioButtonList>
複選框列表的基本語法:
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"> </asp:CheckBoxList>
複選框和單選按鈕列表的常見屬性:
Property | Description |
---|---|
RepeatLayout | This attribute specifies whether the table tags or the normal html flow to use while formatting the list 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 lists and Numbered lists
項目符號列表控制項創建項目符號列表或編號列表。這些控制項包含可以通過控制項的Items屬性引用的ListItem對象的集合。
項目符號列表的基本語法:
<asp:BulletedList ID="BulletedList1" runat="server"> </asp:BulletedList>
項目符號列表的常見屬性:
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
超連結控制項類似於HTML<a>元素。
超連結控制項的基本語法:
<asp:HyperLink ID="HyperLink1" runat="server"> HyperLink </asp:HyperLink>
它具有以下重要特性:
Property | Description |
---|---|
ImageUrl | Path of the image to be displayed by the control. |
NavigateUrl | Target link URL. |
Text | The text to be displayed as the link. |
Target | The window or frame which loads the linked page. |
Image Control
圖像控制項用於在網頁上顯示圖像,或者在圖像不可用時顯示其他文本。
圖像控制項的基本語法:
<asp:Image ID="Image1" runat="server">
它具有以下重要特性: