三.Web服务器控件
I.System.Web.UI.WebControls名称空间结构
II.Web服务器控件的基本属性
属性 | 说明 |
AccessKey | 定义控件的加速键。比如,指定控件的AccessKey属性值为W,用户可以通过按Alt+W来访问该控件。并不是所有的浏览器都支持这个属性 |
Attributes | 控件属性集合。只能在编程时,而不能在声明一个控件时指定这个属性 |
BackColor | 控件的背景色。取值可以用颜色的英文名字,如yellow,也可以用十六进制表示,如 |
BorderWidth | 控件的边界的宽度,单位为pixel。一些较早的浏览器不支持这个属性 |
BorderStyle | 控件边界的样式。可能的取值有:NotSet,None,Dotted,Dashed,Solid,Double,Groove,Ridge,Inset和OutSet |
CSSClass | 控件使用的样式表类 |
CSSStyle | 控件使用的样式表类型的属性的集合。只能在编程时,而不能在声明一个控件时指定这个属性。如果在控件中已经使用了定义外观样式的属性(BackColor),那么在CSSStyle中相应属性就会被覆盖 |
Enabled | 指定控件能否被用户访问,取值为Boolean型 |
Font-Bold | 指定控件上的广西是否为加粗显示,取值为Boolean型,默认值为False |
Font-Italic | 指定控件上的文本是否为斜体显示,取值为Boolean型,默认值为False |
Font-Name | 指定控件上的文本的字体。一些浏览器不支持为单个的控件设置字体 |
Font-Names | 控件上文本字体的一个列表,用于浏览器做优先选择。如果浏览器不能识别指定的字体,则选择列表中的下一个字体 |
Font-Overline | 指定控件上文本的上方是否有一根线段,取值为Boolean型,默认值为False |
Font-size | 字号的大小,取值从1到7 |
Font-Strikeout | 指定控件上文本的中间是否有一根线段穿过,取值为Boolean型,默认值为False |
Font-Underline | 指定控件上文本的下方是否有一根线段,取值为Boolean型,默认值为False |
Forecolor | 指定控件上文本的颜色。一些较早的浏览器不支持此属性 |
Height | 控件的高度,单位为pixel。IE4.0及以上版本支持此属性 |
TabIndex | 指定用户按Tab键时经过控件的顺序,默认值为0。如果这个控件的这个属性具有相同值,则顺序为声明这些控件时的顺序 |
Tooltip | 指定当用户鼠标停留在控件上时显示的提示文字。不是所有的浏览器都支持这个属性 |
Width | 控件的绝对宽度,默认的单位为字符数,也可以使用pixel(100px)或者百分比形式(如40%) |
III.一些控件的支持Style对象的属性
类型 | 属性 | 说明 |
基本类型 | styleobject-BackColor | 控件的背景色,取值可以用颜色的英文名字 |
styleobject-BorderColor | 控件的边框颜色 | |
styleobject-BorderStyle | 控件的边框样式,可能的取值有:NoSet,None,Dtted,Dashed,Solid,Double, Groove,Ridge,Inset和OutSet | |
styleobject-BorderWidth | 控件的边框宽度,单位为pixel | |
styleobject-CssClass | 控件使用的样式表类 | |
styleobject-Font-Bold | 指定控件上的文本是否为加粗显示,取值为Boolean型,默认值为False | |
styleobject-Font-Italic | 指定控件上的文本是否为斜体显示,取值为Boolean型,默认值为False | |
styleobject-Font-Name | 指定控件上的文本的字体 | |
styleobject-Font-Names | 控件上文本字体的一个列表,用于浏览器做优先选择。如果浏览器不能识别指定字体,则选择列表中的下一个字体 | |
styleobject-Font-Overline | 指定控件上文本的上方是否有一根线段,取值为Boolean型,默认值为False | |
styleobject-Font-size | 字号的大小,取值从1到7 | |
styleobject-Font-Strkeout | 指定控件上文本的中间是否有一根线段穿过,取值为Boolean型,默认值为False | |
styleobject-Font-Underline | 指定控件上文本的下方是否有一根线段,取值为Boolean型,默认值为False | |
styleobject-Forecolor | 指定控件上文本的颜色 | |
styleobject-Height | 控件的绝对高度 | |
styleobject-Width | 控件的绝对宽度 | |
styleobject.IsEmpty | 判断对象是否为空。只能在编程时使用该属性 | |
数据表格页面导航类型 | PagerStyle-Mode | 指定页面导航标志使用的模式,可取值有NextPrev和NumericPages。默认值为NextPrev |
PagerStyle-NexPageText | 指定导航标志的导航到下一页按钮使用的文本 | |
PagerStyle-PageButton-Count | 当PagerStyle-Mode属性的取值为NumericPages时,指定在导航标志中显示的页的数量 | |
PagerStyle-Position | 指定页面导航标志相对于其关联控件的位置,可取值为Bottom,Top和TopAndDottom | |
PagerStyle-PrevPageText | 指定导航标志的导航到上一页的按钮使用的文本 | |
PagerStyle-Visible | 指定页面导航标志是否可见,取值为Boolean型,默认值为True | |
表格项目类型 | styleobject-HorizontalAlign | 指定对象包含内容的水平排列方式,可取值有Center,Justify,Left,NotSet和Top。默认值为NotSet |
styleobject-VerticalAlign | 指定对象包含内容的竖直排列方式,可取值有Bottom,Middle,NotSet和Top,默认值为NotSet | |
styleobject-Wrap | 指定对象的内容超过对象宽度时是否可以换行显示,默认值为True |
IV.Web服务器控件的基本事件
事件 | 对应的Web控件 |
Click,Command | Button,ImageButton |
TextChange | TextBox |
CheckChanged | CheckBox,RadioButton |
SelectIndexChanged | DropDownList,ListBox,CheckBoxList ,RadioButtonList |
V.基本控件和数据控件与Html标识对应关系
类名 | Html标识表示 |
Button | <input type=button>, <input type=submit> |
CheckBox | <input type=checkbox> |
HyperLink | <a> |
Image | <img> |
ImageButton | <input type=image> |
Label | <span> |
LinkButton,literal | 带有<img>标记的<a> |
Panel,PlaceHolder | <div> |
RadioButton | <input type=radio> |
Table,TableCell | <table><td><th> |
TableRow | <tr> |
TextBox | <input type=text>, <input type=password><textarea> |
DropDownList | <select> |
ListBox | <select size=N>,N是一次可见的行数 |
CheckboxList | 一个列表或带有多个 <input type=checkbox>的<table> |
RadioButtonList | 一个列表或带有多个 <input type=radio>的<table> |
VI.控件分类
1.用于文本输入和显示的内部控件
(1).TextBox (类似文本输入框)
<asp:textbox options></asp:textbox>
或者: <asp:textbox options/>
属性/事件名 | 取值 | 说明 |
Id | 任意字符串 | ID标识 |
Runat | Server | 该控件是服务器控件 |
AutoPostBack | True/False | 是否允许自动回传事件到服务器 |
Columns | 数字 | 本控件的宽度 |
MaxLength | 数字 | 最多输入字符数(TextMode<>MultiLine) |
Rows | 数字 | 行数(TextMode=MultiLine) |
Text | 字符串 | 初始化文本 |
TextMode | MultiLine/Password | 显示模式 MultiLine:多行输入文本 Password:密码文本 不设置表示单行文本 |
Wrap | True/False | 是否允许自动换行(TextMode=MultiLine) |
OnTextChanged | 处理事件的方法名 | 当控件内部的文本发生变化时回传到服务器的事件处理方法 |
例:
<Form Id="Form1" Runat="Server">
<ASP:Textbox Id="T1" AutoPostBack="True" OnTextChanged="T1_Changed" Runat="server" /><p>
<ASP:Label Id="Label1" Runat="Server" />
</Form>
<Script Language="VB" Runat="Server">
Sub Page_Load(Sender As Object,e As Eventargs)
Label1.Text="文字的内容没有改变"
End Sub
Sub T1_Changed(Sender As Object,e As Eventargs)
Label1.Text="文字的内容已经改变"
End Sub
</Script>
(2).Label (类似文本显示标签)
<asp:label options>text</asp:label>
或者: <asp:label options/>
属性名 | 取值 | 说明 |
Id | 任意字符串 | 用于唯一表示Label控件的字符串 |
Runat | Server | 表示这个Label控件是服务器端控件 |
Text | 字符串 | 初始始显示时的字符串 |
例1:文本标签的字体设为黑体
lblTxt.font.bold=True
lblTxt.font.bold=False
例2:
<ASP:Label Id="Label0" Text="我的背景是红色" BackColor="red" Runat="Server"/>
<ASP:Label Id="Label1" Runat="Server" Font-Bold="True" Text="粗体"/>
<ASP:Label Id="Label2" Runat="Server" Font-Italic="True" Text="斜体"/>
<ASP:Label Id="Label3" Runat="Server" Font-Strikeout="True" Text="删除线"/>
<ASP:Label Id="Label4" Runat="Server" Font-Underline="True" Text="底线"/>
<ASP:Label Id="Label5" Runat="Server" Font-Size="XX-Large" Text="大字体"/>
(3).Literal :把普通文本放到页面上而不包含任何HTML标记
<asp:Literal options>text</asp:Literal>
或者: <asp:Literal options/>
属性名 | 取值 | 说明 |
Id | 任意字符串 | 用于唯一表示Literal控件的字符串 |
Runat | Server | 表示这个Literal控件是服务器端控件 |
Text | 字符串 | 初始始显示时的字符串,可以包含HTML标识,以进一步格式化Text文本 |
2.用于控件传送的内部控件
(1).Button (类似普通按钮)
<asp:button options></asp:button>
或者: <asp:button options/>
属性名 | 取值 | 说明 |
Id | 任意字符串 | 唯一标识 |
Runat | Server | 服务器端控件 |
Text | 字符串 | 控件上显示的文本 |
AccessKey | A-Z/a-z | 按Alt+取值实现同样的功能 |
OnClick和Command事件 |
| 当单击按钮时出现。这二个事件之间的惟一区别是Command事件接收额外的信息,即CommandName和CommandArgument属性 |
CommandName和CommandArgument |
| 提供给Command事件的额外的信息。在事件处理程序正在从多个不同的按钮控件接收事件的情况下,它能允许惟一地标识按钮。commandName是一个字符串,用于表示动作(如,Sort),而Commandargument存储任何附加的信息(如,排序的字段等) |
例1:
<asp:button id="btn2" text="OnMouseOver事件演示" Width=150px
OnMouseOver="this.style.backgroundColor='lightgreen'"
OnMouseOut="this.style.backgroundColor='buttonface'" runat=server />
<br>
<asp:button id="btn3" text="OnMouseOut事件演示" Width=150px
OnMouseOver="this.style.fontWeight='bold'" OnMouseOut="this.style.fontWeight='normal'" runat=server />
例2:<ASP:Button Id="Button1" Text="按钮一(A)" Runat="Server" AccessKey="a" OnClick="Button1_Click"/>
例3:通过OnCommand事件,获取CommandArgument及CommandName属性
<Form Id="Form1" Runat="Server">
<ASP:Button Id="Button1" text="MyButton" CommandArgument="CA属性" CommandName="CN属性"
OnCommand="Button1_Click" Runat="Server" /><p>
<ASP:Label Id="Label1" Runat="Server" /><br>
<ASP:Label Id="Label2" Runat="Server" />
</Form>
<Script Language="VB" Runat="Server">
Sub Button1_Click(Sender As Object,e As CommandEventArgs)
Label1.Text=e.CommandArgument.toString
Label2.Text=e.CommandName.toString
End Sub
</Script>
(2).LinkButton (类似超链接控件)
<asp:linkbutton options>text<asp:linkbutton>
或者: <asp:linkbutton options/>
属性名 | 取值 | 说明 |
Id | 任意字符串 | 唯一标识 |
Runat | Server | 服务器端控件 |
Text | 字符串 | 控件上显示的文本 |
CauseseValidation |
| 如果设置为True(默认值),当单击图像时,页面将使用表单上的所有验证控件自动验证。取决于浏览器的性能,无效的页面可能不被传回 |
OnClick和Command事件 |
| 当单击按钮时出现。这二个事件之间的惟一区别是Command事件接收额外的信息,即CommandName和CommandArgument属性 |
CommandName和CommandArgument |
| 提供给Command事件的额外的信息。在事件处理程序正在从多个不同的按钮控件接收事件的情况下,它能允许惟一地标识按钮。commandName是一个字符串,用于表示动作(如,Sort),而Commandargument存储任何附加的信息(如,排序的字段等) |
例1:通过OnCommand事件,获取CommandArgument及CommandName属性
<Form Id="Form1" Runat="Server">
<ASP:LinkButton Id="Button1" text="MyLButton" CommandArgument="CA属性" CommandName="CN属性"
OnCommand="Button1_Click" Runat="Server" /><p>
<ASP:Label Id="Label1" Runat="Server" /><br>
<ASP:Label Id="Label2" Runat="Server" />
</Form>
<Script Language="VB" Runat="Server">
Sub Button1_Click(Sender As Object,e As CommandEventArgs)
Label1.Text=e.CommandArgument.toString
Label2.Text=e.CommandName.toString
End Sub
</Script>
(3).ImageButton (类似图片显示框)
<asp:imagebutton options></asp:imagebutton>
或者: <asp:imagebutton options/>
属性名 | 取值 | 说明 |
Id | 任意字符串 | 唯一标识 |
Runat | Server | 服务器端控件 |
ImageURL | URL字符串 | 表示图片的URL字符串 |
AlternateText | 字符串 | 将显示在不支持图像的浏览器中的文本,以及当用户鼠标停留在图片上时,显示在Internet Exlorer中弹出的ToolTip文本 |
ImageAlign |
| 相对于页面和它本身的文本对齐特性(取值为Left,Right,Middle,Top,bottom或Texttop) |
CauseseValidation |
| 如果设置为True(默认值),当单击图像时,页面将使用表单上的所有验证控件自动验证。取决于浏览器的性能,无效的页面可能不被传回 |
OnClick和OnCommand事件 |
| 当单击按钮时出现。这二个事件之间的惟一区别是Command事件接收额外的信息,即CommandName和CommandArgument属性 |
CommandName和CommandArgument |
| 提供给Command事件的额外的信息。在事件处理程序正在从多个不同的按钮控件接收事件的情况下,它能允许惟一地标识按钮。commandName是一个字符串,用于表示动作(如,Sort),而Commandargument存储任何附加的信息(如,排序的字段等) |
例1:
<Form Id="Form1" Runat="Server">
<ASP:ImageButton Id="Button1" ImageUrl="vsdotnet.gif" Onclick="Button1_Click" Runat="Server" /><p>
<ASP:Label Id="Label1" Runat="Server" />
</Form>
<Script Language="VB" Runat="Server">
Sub Button1_Click(Sender As Object,e As ImageClickEventArgs)
Label1.Text="您位于影像的" & e.x.ToString & ", " & e.y.ToString & _
" 的位置按下鼠标"
End Sub
</Script>
例2:通过OnCommand事件,获取CommandArgument及CommandName属性
<Form Id="Form1" Runat="Server">
<ASP: ImageButton Id="Button1" text="MyIButton" CommandArgument="CA属性" CommandName="CN属性"
ImageUrl="vsdotnet.gif" OnCommand="Button1_Click" Runat="Server" /><p>
<ASP:Label Id="Label1" Runat="Server" /><br>
<ASP:Label Id="Label2" Runat="Server" />
</Form>
<Script Language="VB" Runat="Server">
Sub Button1_Click(Sender As Object,e As CommandEventArgs)
Label1.Text=e.CommandArgument.toString
Label2.Text=e.CommandName.toString
End Sub
</Script>
(4).HyperLink (类似超链接控件)
<asp:hyperlink options>text</asp:hyperlink>
或者: <asp:hyperlink options/>
属性名 | 取值 | 说明 |
Id | 任意字符串 | 唯一标识 |
Runat | Server | 服务器端控件 |
Text | 字符串 | 描述这个链接的字符串 |
NavigateUrl | URL字符串 | 当用户单击这个链接时转向的URL |
ImageUrl | URL字符串 | 当用一个图片代替文本来描述链接的时候,这个图片的URL。图像优先于文本,即,除非图像有问题 |
Target | 字符串 | 当用户单击这个链接的时候,显示目标页面的窗口的名称。取值为_blank,_parent,_self,_top |
(5).Image
<asp:Image Ontions/>
属性名 | 说明 |
AlternateText | 将显示在不支持图像的浏览器中的文本,以及当用户鼠标停留在图片上时,显示在Internet Exlorer中弹出的ToolTip文本 |
ImageAlign | 图像相对于页面和它本身的文本对齐特性(取值为Left,Right,Middle,Top,Bottom或Texttop) |
ImageUrl | 显示图像的路径和文件名,可以是绝对、相对、或虚拟的路径 |