📅  最后修改于: 2020-11-21 05:43:48             🧑  作者: Mango
在本章中,我们将讨论ASP.NET中可用的基本控件。
ASP.NET提供三种类型的按钮控件:
按钮:在矩形区域内显示文本。
链接按钮:显示看起来像超链接的文本。
图像按钮:显示图像。
用户单击按钮时,将引发两个事件:单击和命令。
按钮控制的基本语法:
按钮控件的常用属性:
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. |
文本框控件通常用于接受用户的输入。文本框控件可以接受一行或多行文本,具体取决于TextMode属性的设置。
标签控件提供了一种显示文本的简便方法,可以将文本从一次执行更改为下一次执行。如果要显示不变的文本,请使用字面量文本。
文字控制的基本语法:
文本框和标签的共同属性:
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. |
标签控件最常用的属性是“文本”,这表示标签上显示的文本。
复选框显示一个选项,用户可以选中或取消选中它,单选按钮显示一组选项,用户可以从中选择一个选项。
要创建一组单选按钮,请为组中每个单选按钮的GroupName属性指定相同的名称。如果以一种形式需要多个组,则为每个组指定一个不同的组名称。
如果要在初次显示表单时选择复选框或单选按钮,请将其Checked属性设置为true。如果组中的多个单选按钮的Checked属性设置为true,则仅将最后一个按钮视为true。
复选框的基本语法:
单选按钮的基本语法:
复选框和单选按钮的共同属性:
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. |
ASP.NET提供以下控件
这些控件使用户可以从列表中选择一项或多项。列表框和下拉列表包含一个或多个列表项。这些列表可以通过代码或ListItemCollection编辑器加载。
列表框控件的基本语法:
下拉列表控件的基本语法:
列表框和下拉列表的共同属性:
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. |
重要的是要注意:
若要使用下拉列表或列表框中的项目,请使用控件的Items属性。此属性返回一个ListItemCollection对象,其中包含列表的所有项目。
当用户从下拉列表或列表框中选择其他项目时,将引发SelectedIndexChanged事件。
ListItemCollection对象是ListItem对象的集合。每个ListItem对象代表列表中的一项。 ListItemCollection中的项目从0开始编号。
当使用诸如lstcolor.Items.Add(“ Blue”)之类的字符串将项目加载到列表框中时,列表项目的Text和Value属性都将设置为您指定的字符串值。要对其进行不同的设置,必须创建一个列表项对象,然后将该项目添加到集合中。
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. |
单选按钮列表显示互斥选项的列表。复选框列表显示了独立选项的列表。这些控件包含ListItem对象的集合,可以通过控件的Items属性来引用它们。
单选按钮列表的基本语法:
复选框列表的基本语法:
复选框和单选按钮列表的共同属性:
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. |
项目符号列表控件创建项目符号列表或编号列表。这些控件包含ListItem对象的集合,可以通过控件的Items属性来引用它们。
项目符号列表的基本语法:
项目符号列表的常见属性:
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控件类似于HTML 元素。
超链接控件的基本语法:
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. |
如果图像不可用,则图像控件用于在网页上显示图像,或在某些替代文本上显示图像。
图像控件的基本语法:
它具有以下重要属性:
Property | Description |
---|---|
AlternateText | Alternate text to be displayed in absence of the image. |
ImageAlign | Alignment options for the control. |
ImageUrl | Path of the image to be displayed by the control. |