三.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 | 顯示圖像的路徑和檔案名,可以是絕對、相對、或虛拟的路徑 |