国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

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

2024-07-10 13:10:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

  服務(wù)器控件是在.aspx文件中使用自定義標(biāo)記或固有的html標(biāo)記聲明的,它包含了runat="server"屬性值。固有的html標(biāo)記是由system.web.ui.htmlcontrols名字空間中的一個(gè)控件來(lái)處理的。沒(méi)有顯式地映射到某個(gè)控件的標(biāo)記會(huì)被指定為system.web.ui.htmlcontrols.htmlgenericcontrol類型。

  下面的例子使用了四個(gè)服務(wù)器控件:<form runat=server>、<asp:textbox runat=server>、<asp:dropdownlist runat=server>和<asp:button runat=server>。在運(yùn)行的時(shí)候這些服務(wù)器控件自動(dòng)地生成html內(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>

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

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

<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ù)器控件事件

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

  下面的例子演示了asp.net頁(yè)面開(kāi)發(fā)者如何處理<asp:button runat=server>控件的onclick事件來(lái)改變<asp:label runat=server>控件的text屬性的。

<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>

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

  使用服務(wù)器控件

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

<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ù)器控件

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

<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ù)器事件,以供頁(yè)面開(kāi)發(fā)者處理。頁(yè)面開(kāi)發(fā)者可以通過(guò)宣告式地給每個(gè)控件編寫事件來(lái)實(shí)現(xiàn)這項(xiàng)功能(在這種情況下,事件的屬性名稱表明事件的名稱,屬性的值表明被調(diào)用的方法的名稱)。例如,下面的代碼示例演示了如何給按鈕控件編寫onclick事件。

<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>

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

  事件處理程序?yàn)轫?yè)面開(kāi)發(fā)者在asp.net頁(yè)面中構(gòu)造邏輯提供了一條清晰的途徑。例如,下面的例子演示了如何在一個(gè)頁(yè)面上處理四個(gè)按鈕事件。

<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í)行頁(yè)面導(dǎo)航(第一種情況)

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

<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í)行頁(yè)面導(dǎo)航(第二種情況)

  并非所有的頁(yè)面導(dǎo)航都由客戶端的超級(jí)鏈接發(fā)起。asp.net頁(yè)面開(kāi)發(fā)者調(diào)用response.redirect(url)方法也可以發(fā)起客戶端頁(yè)面的重定向或?qū)Ш健_@種情況典型發(fā)生在真正進(jìn)行導(dǎo)航之前,服務(wù)器端需要驗(yàn)證客戶端的輸入信息的時(shí)候。

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

<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>
 <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" forecolor="red" font-bold="true" runat=server/>
 </font>
</form>
</body>
</html>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 英山县| 昭觉县| 广昌县| 定襄县| 建湖县| 绥滨县| 湖口县| 信宜市| 盐津县| 谢通门县| 镇江市| 神池县| 宣汉县| 治多县| 台东县| 余庆县| 丹巴县| 黄浦区| 玉屏| 雅安市| 崇文区| 额敏县| 合川市| 洮南市| 略阳县| 勐海县| 长宁区| 厦门市| 永善县| 庆阳市| 安平县| 三门县| 广德县| 赣州市| 龙江县| 工布江达县| 蒲江县| 巨鹿县| 武鸣县| 视频| 汉寿县|