日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

ASP.NET 2.0中的Web和HTML服務(wù)器控件_.Net教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:ASP.NET應(yīng)用程序資源訪問安全模型
1、前言 ASP.NET WEB應(yīng)用程序通常屬于多層體系結(jié)構(gòu),一般從邏輯結(jié)構(gòu)上可以分為表示層、業(yè)務(wù)邏輯層和數(shù)據(jù)訪問層;客戶端要訪問應(yīng)用程序資源,其身份認(rèn)證和授權(quán)必然要跨越多個層次。本文主要討

除了代碼和標(biāo)記之外,ASP.NET 2.0頁面還可以包含服務(wù)器控件,它們是可編程的服務(wù)器端對象,典型情況下表現(xiàn)為頁面中的UI元素(例如文本框或圖像)。服務(wù)器控件參與頁面的執(zhí)行過程,并給客戶端生成自已的標(biāo)記呈現(xiàn)內(nèi)容。服務(wù)器控件的優(yōu)勢在于,它讓開發(fā)者從簡單的積木式的組件中獲取復(fù)雜的呈現(xiàn)方式和操作行為,極大地減少了生成動態(tài)Web頁面所需要編寫的代碼量;另外一個優(yōu)勢是,定制它們的呈現(xiàn)方式和行為非常簡單。服務(wù)器控件所暴露的屬性可以通過宣告式(在標(biāo)記中)或編程(在代碼中)設(shè)置。服務(wù)器控件(和頁面控件本身)還暴露了一些事件,開發(fā)者可以處理這些事件,在頁面執(zhí)行的過程中,或者響應(yīng)向服務(wù)器發(fā)回頁面的客戶端操作(Postback)的時候,所需來執(zhí)行的特定操作。服務(wù)器控件還簡化了保留狀態(tài)信息的問題,它會自動地在多個成功的“發(fā)回” 操作之間保留值。

服務(wù)器控件是在.aspx文件中使用自定義標(biāo)記或固有的HTML標(biāo)記聲明的,它包含了runat="server"屬性值。固有的HTML標(biāo)記是由System.Web.UI.HtmlControls名字空間中的一個控件來處理的。沒有顯式地映射到某個控件的標(biāo)記會被指定為System.Web.UI.HtmlControls.HtmlGenericControl類型。

下面的例子使用了四個服務(wù)器控件:<form runat=server>、<asp:textbox runat=server>、<asp:dropdownlist runat=server>和<asp:button runat=server>。在運行的時候這些服務(wù)器控件自動地生成HTML內(nèi)容。

以下為引用的內(nèi)容:

<form action="intro4_VB.aspx" method="post" runat=server>
<h3> Name: <asp:textbox id="Name" runat="server"/>
Category: <asp:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>
</h3>
<asp:button text="Lookup" runat="server"/>
</form>

請注意:這些服務(wù)器控件自動地保留了往返于服務(wù)器之間的客戶端所輸入的值。這些控件狀態(tài)并非存儲在服務(wù)器上(它們存儲在往返于請求之間的<input type="hidden">窗體字段中)。它不需要客戶端腳本。

除了支持標(biāo)準(zhǔn)的HTML輸入控件之外,ASP.NET還允許開發(fā)者在頁面中使用豐富的定制控件。例如,下面的例子演示了如何使用<asp:adrotator>控件在頁面上動態(tài)地顯示滾動廣告。

以下為引用的內(nèi)容:

<form action="intro5_VB.aspx" method="post" runat="server">
<asp:adrotator AdvertisementFile="ads.XML" BorderColor="black" BorderWidth=1 runat="server"/>
<h3> Name: <asp:textbox id="Name" runat="server"/>
Category: <asp:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>
</h3>
<asp:button text="Lookup" runat="server"/>
</form>

處理服務(wù)器控件事件

每個ASP.NET服務(wù)器控件都能夠暴露一個對象模型,它包含了屬性、方法和事件。ASP.NET開發(fā)者可以使用這個對象模型清晰地修改頁面、與頁面交互操作。

下面的例子演示了ASP.NET頁面開發(fā)者如何處理<asp:button runat=server>控件的OnClick事件來改變<asp:label runat=server>控件的Text屬性的。

以下為引用的內(nèi)容:

<html>
<head>
<link rel="stylesheet"href="intro.css">
</head>

<script language="VB" runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Message.Text = "Hi " & HttpUtility.HtmlEncode(Name.Text) & ", you selected: " & Category.SelectedItem.Text
End Sub
</script>

<body>
<center>
<form action="intro6_VB.aspx" method="post" runat="server">
<asp:adrotator AdvertisementFile="ads.XML" BorderColor="black" BorderWidth=1 runat="server"/>
<h3> Name: <asp:textbox id="Name" runat="server"/>
Category: <asp:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>
</h3>
<asp:button text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>
<p>
<asp:label id="Message" runat="server"/>
</form>
</center>
</body>
</html>

這個簡單的例子與前面演示的“Intro3”示例功能相當(dāng)。請注意,在這個新的基于服務(wù)器控件的例子中,代碼變得非常清晰和簡單了。我們以后還將看到,ASP.NET頁面框架組件也暴露了大量的頁面層次的事件,在頁面的處理過程中,你可以編寫在特定時間執(zhí)行的代碼。這些事件包括Page_Load和Page_Render。

使用服務(wù)器控件

ASP.NET服務(wù)器控件是在頁面中使用包含runat="server"屬性的宣告式標(biāo)記來定義的。下面的例子聲明了三個<asp:label runat="server">服務(wù)器控件,并定義了每個控件的文本和樣式屬性。

以下為引用的內(nèi)容:

<html>
<body>
<h3><font face="Verdana">Declaring Server Controls</font></h3>
This sample demonstrates how to declare the <asp:label> server control and
manipulate its properties within a page.
<p>
<hr>
<asp:label id="Message1" font-size="16" font-bold="true" forecolor="red" runat=server>This is Message One</asp:label>
<br>
<asp:label id="Message2" font-size="20" font-italic="true" forecolor="blue" runat=server>This is Message Two</asp:label>
<br>
<asp:label id="Message3" font-size="24" font-underline="true" forecolor="green" runat=server>This is Message Three</asp:label>
</body>
</html>

操作服務(wù)器控件

你可以用編程的方式,通過提供ASP.NET服務(wù)器控件的id屬性來識別服務(wù)器控件;還可以在運行時刻,使用這個id指針來編程操作該服務(wù)器控件的對象模型。例如,下面的例子演示了頁面開發(fā)者如何在Page_Load事件中編程設(shè)置<asp:label runat="server">控件的Text屬性。

以下為引用的內(nèi)容:

<html>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Message.Text = "You last ACCESSed this page at: " & DateTime.Now
End Sub
</script>

<body>
<h3><font face="Verdana">Manipulating Server Controls</font></h3>
This sample demonstrates how to manipulate the <asp:label> server control within
the Page_Load event to output the current time.
<p>
<hr>
<asp:label id="Message" font-size="24" font-bold="true" runat=server/>
</body>
</html>

處理控件的事件

ASP.NET服務(wù)器控件也可以暴露和引發(fā)服務(wù)器事件,以供頁面開發(fā)者處理。頁面開發(fā)者可以通過宣告式地給每個控件編寫事件來實現(xiàn)這項功能(在這種情況下,事件的屬性名稱表明事件的名稱,屬性的值表明被調(diào)用的方法的名稱)。例如,下面的代碼示例演示了如何給按鈕控件編寫OnClick事件。

以下為引用的內(nèi)容:

<html>
<script language="VB" runat="server">
Sub EnterBtn_Click(Sender As Object, E As EventArgs)
Message.Text = "Hi " & Name.Text & ", welcome to ASP.NET!"
End Sub
</script>

<body>
<h3><font face="Verdana">Handling Control Action Events</font></h3>
<p>
This sample demonstrates how to ACCESS a <asp:textbox> server control within the "Click" event of a <asp:button>, and use its content to modify the text of a <asp:label>.
<p>
<hr>

<form action="controls3.aspx" runat=server>
<font face="Verdana"> Please enter your name:
<asp:textbox id="Name" runat=server/>
<asp:button text="Enter" Onclick="EnterBtn_Click" runat=server/>
<p>
<asp:label id="Message" runat=server/>
</font>
</form>

</body>
</html>

處理多個服務(wù)器事件

事件處理程序為頁面開發(fā)者在ASP.NET頁面中構(gòu)造邏輯提供了一條清晰的途徑。例如,下面的例子演示了如何在一個頁面上處理四個按鈕事件。

以下為引用的內(nèi)容:

<html>
<script language="VB" runat="server">
Sub AddBtn_Click(Sender As Object, E As EventArgs)
If Not (AvailableFonts.SelectedIndex = -1)
InstalledFonts.Items.Add(New ListItem(AvailableFonts.SelectedItem.Value))
AvailableFonts.Items.Remove(AvailableFonts.SelectedItem.Value)
End If
End Sub

Sub AddAllBtn_Click(Sender As Object, E As EventArgs)
Do While Not (AvailableFonts.Items.Count = 0)
InstalledFonts.Items.Add(New ListItem(AvailableFonts.Items(0).Value))
AvailableFonts.Items.Remove(AvailableFonts.Items(0).Value)
Loop
End Sub

Sub RemoveBtn_Click(Sender As Object, E As EventArgs)
If Not (InstalledFonts.SelectedIndex = -1)
AvailableFonts.Items.Add(New ListItem(InstalledFonts.SelectedItem.Value))
InstalledFonts.Items.Remove(InstalledFonts.SelectedItem.Value)
End If
End Sub

Sub RemoveAllBtn_Click(Sender As Object, E As EventArgs)
Do While Not (InstalledFonts.Items.Count = 0)
AvailableFonts.Items.Add(New ListItem(InstalledFonts.Items(0).Value))
InstalledFonts.Items.Remove(InstalledFonts.Items(0).Value)
Loop
End Sub
</script>
<body>
<h3><font face="Verdana">Handling Multiple Control Action Events</font></h3>
<p>
This sample demonstrates how to handle multiple control action events raised from
different <asp:button> controls.
<p>
<hr>

<form action="controls4.aspx" runat=server>
<table>
<tr>
<td>
Available Fonts
</td>
<td>
<!-- Filler -->
</td>
<td>
Installed Fonts
</td>
</tr>
<tr>
<td>
<asp:listbox id="AvailableFonts" width="100px" runat=server>
<asp:listitem>Roman</asp:listitem>
<asp:listitem>Arial Black</asp:listitem>
<asp:listitem>Garamond</asp:listitem>
<asp:listitem>Somona</asp:listitem>
<asp:listitem>Symbol</asp:listitem>
</asp:listbox>
</td>
<td>
<!-- Filler -->
</td>
<td>
<asp:listbox id="InstalledFonts" width="100px" runat=server>
<asp:listitem>Times</asp:listitem>
<asp:listitem>Helvetica</asp:listitem>
<asp:listitem>Arial</asp:listitem>
</asp:listbox>
</td>
</tr>
<tr>
<td>
<!-- Filler -->
</td>
<td>
<asp:button text="<<" OnClick="RemoveAllBtn_Click" runat=server/>
<asp:button text="<" OnClick="RemoveBtn_Click" runat=server/>
<asp:button text=">" OnClick="AddBtn_Click" runat=server/>
<asp:button text=">>" OnClick="AddAllBtn_Click" runat=server/>
</td>
<td>
<!-- Filler -->
</td>
</tr>
</table>
</form>
</body>
</html>

執(zhí)行頁面導(dǎo)航(第一種情況)

在實際的Web應(yīng)用程序中,多個頁面之間的導(dǎo)航是常見的。下面的例子演示了如何使用<asp:hyperlink runat=server>控件導(dǎo)航到另外一個頁面(同時傳遞了自定義的查詢字符串參數(shù))。接著這個例子演示了如何輕易地在目標(biāo)頁面上得到這些查詢字符串參數(shù)。

以下為引用的內(nèi)容:

<html>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim RandomGenerator As Random
RandomGenerator = New Random(DateTime.Now.Millisecond)
Dim RandomNum As Integer
RandomNum = RandomGenerator.Next(0, 3)
Select RandomNum
Case 0:
Name.Text = "Scott"
Case 1:
Name.Text = "Fred"
Case 2:
Name.Text = "Adam"
End Select
AnchorLink.NavigateUrl = "controls_navigationtarget_VB.aspx?name=" & System.Web.HttpUtility.UrlEncode(Name.Text)
End Sub
</script>
<body>
<h3><font face="Verdana">Performing Page Navigation (Scenario 1)</font></h3>
<p>
This sample demonstrates how to generate a HTML Anchor tag that will cause the client to
navigate to a new page when he/she clicks it within the browser.
<p>
<hr>
<p>
<asp:hyperlink id="AnchorLink" font-size=24 runat=server>
Hi <asp:label id="Name" runat=server/> please click this link!
</asp:hyperlink>
</body>
</html>

執(zhí)行頁面導(dǎo)航(第二種情況)

并非所有的頁面導(dǎo)航都由客戶端的超級鏈接發(fā)起。ASP.NET頁面開發(fā)者調(diào)用Response.Redirect(url)方法也可以發(fā)起客戶端頁面的重定向或?qū)Ш�。這種情況典型發(fā)生在真正進行導(dǎo)航之前,服務(wù)器端需要驗證客戶端的輸入信息的時候。

下面的例子演示了如何使用Response.Redirect方法把參數(shù)傳遞到另外一個目標(biāo)頁面。它還演示了如何在目標(biāo)頁面上簡單地獲取這些參數(shù)。

以下為引用的內(nèi)容:

<html>
<script language="VB" runat="server">
Sub EnterBtn_Click(Sender As Object, E As EventArgs)
 If Not (Name.Text = "")
  Response.Redirect("Controls_NavigationTarget_VB.aspx?name=" & System.Web.HttpUtility.UrlEncode(Name.Text))
 Else
  Message.Text = "Hey! Please enter your name in the textbox!"
 End If
End Sub
</script>
<body>
<h3><font face="Verdana">Performing Page Navigation (Scenario 2)</font></h3>
<p>
This sample demonstrates how to navigate to a new page from within a <asp:button> click event, passing a <asp:textbox> value as a querystring argument (validating first that the a legal textbox value has been specified).
<p>
<hr>
<form action="controls6.aspx" runat=server>
�。糵ont face="Verdana">Please enter your name:
  <asp:textbox id="Name" runat=server/>
 �。糰sp:button text="Enter" Onclick="EnterBtn_Click" runat=server/>
 �。紁>
  <asp:label id="Message" forecolor="red" font-bold="true" runat=server/>
�。�/font>
</form>
</body>
</html>

分享:ASP.NET 2.0中執(zhí)行數(shù)據(jù)庫操作命令之一
數(shù)據(jù)庫命令執(zhí)行時使用Command對象。Command類有三種:SqlCommand、OleDbCommand與OdbcCommand。 Command對象主要用來運行SELECT、INSERT、UPDATE或DELETE之類的SQL語句。Command對象還可以調(diào)

來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
相關(guān).Net教程