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

首頁 > 編程 > .NET > 正文

ASP.NET 2.0 Web窗體語法指導(dǎo)

2024-07-10 13:10:06
字體:
供稿:網(wǎng)友

  asp.net web窗體頁面是一個宣告式的文本文件,擴(kuò)展名是.aspx。除了靜態(tài)的內(nèi)容之外,你還可以使用八種不同的語法標(biāo)記元素。這一部分回顧這些語法元素并提供了一些使用方法示例。

  呈現(xiàn)代碼的語法:<% %>和<%= %>

  代碼呈現(xiàn)塊用<% ... %>元素表示,它允許你控制呈現(xiàn)的內(nèi)容,在web窗體頁面執(zhí)行的顯示階段執(zhí)行。下面的例子演示了如何使用它們循環(huán)顯示html的內(nèi)容。

<%@ page language="vb" %>
<html>
<body>
<% dim i as integer
for i = 0 to 7 %>
<font size="<%=i%>"> hello world! </font> <br>
<% next %>
</body>
</html>

  <% ... %>包含的代碼只是執(zhí)行,而包含等號(<%= ... %>)的表達(dá)式會在顯示內(nèi)容的時候計算結(jié)果。因此,<%="hello world" %>與c#代碼<% response.write("hello world"); %>顯示的結(jié)果相同。
請注意,由于語言需要使用標(biāo)記來終止或分離語句(例如c#中的分號;),正確地放置這些標(biāo)記就很重要了。

  c# 代碼

  <% response.write("hello world"); %> 需要用分號來終止語句。

<%="hello world"; %> 錯誤:導(dǎo)致"response.write("hello world";);"。

  <%="hello world" %> 不需要分號。

  聲明代碼的語法:<script runat="server">

  代碼聲明塊定義了會被編譯到page類中的成員變量和方法。這些塊可用于建立頁面和導(dǎo)航邏輯。下面的例子演示了如何在<script runat="server">塊中定義subtract方法,接著在頁面中調(diào)用它。

<html>
<script language="vb" runat=server>
function subtract(num1 as integer, num2 as integer) as integer
return num1-num2
end function
</script>

<body>
<%
dim number as integer = 100
do while number > 0
response.write("value: " & number & "<br>")
number = subtract(number, 1)
loop
%>
</body>
</html>

  請注意:與asp不同——在asp中函數(shù)必須在<% %>塊中定義——所有的函數(shù)和全局變量必須使用<script runat=server>標(biāo)記定義。<% %>塊中的函數(shù)聲明會提示語法編譯錯誤信息。

  服務(wù)器控件語法

  定制的asp.net服務(wù)器控件允許頁面開發(fā)者動態(tài)地生成html用戶界面并響應(yīng)客戶端請求。它們是在文件中用宣告式的、基于標(biāo)記的語法表示的。這些標(biāo)記不同于其它的一些標(biāo)記,它們包含一個"runat=server"屬性。下面的例子演示了如何在asp.net頁面中使用<asp:label runat="server">服務(wù)器控件。這個控件與system.web.ui.webcontrols名字空間中的label類對應(yīng)。

  通過添加一個id為“message”的標(biāo)記,可以在運(yùn)行時建立一個label實例:

<asp:label id="message" font-size=24 runat="server"/>

  我們可以使用這個名字來訪問該控件。下面的代碼設(shè)置了該控件的text屬性。

message.text = "welcome to asp.net"
<html>
<script language="vb" runat=server>
sub page_load(sender as object, e as eventargs)
message.text = "welcome to asp.net"
end sub
</script>
<body>
<asp:label id="message" font-size=24 runat=server/>
</body>
</html>

  html服務(wù)器控件語法

  html服務(wù)器控件允許開發(fā)者編程操作頁面中的html元素。html服務(wù)器控件標(biāo)簽與客戶端html元素是有區(qū)別的,它帶有"runat=server"屬性。下面的例子演示了如何在asp.net頁面中使用html<span runat=server>服務(wù)器控件。

<html>
<script language="vb" runat=server>
sub page_load(sender as object, e as eventargs)
message.innerhtml = "welcome to asp.net"
end sub
</script>
<body>
<span id="message" runat=server/>
</body>
</html>

  數(shù)據(jù)綁定語法:<%# %>

  asp.net內(nèi)建的支持?jǐn)?shù)據(jù)綁定的能力允許頁面開發(fā)者分層次地把控件屬性綁定到數(shù)據(jù)容器值。<%# %>代碼塊中的代碼只在自己的父控件容器的databind方法被調(diào)用的時候才執(zhí)行。下面的例子演示了如何在<asp:datalist runat=server>控件中使用數(shù)據(jù)綁定語法。

  在這個數(shù)據(jù)列表中,每個項都指定了模板。項模板的內(nèi)容是使用數(shù)據(jù)綁定表達(dá)式指定的,container.dataitem指向mylist數(shù)據(jù)列表使用的數(shù)據(jù)源。

<asp:datalist id="mylist" runat=server>
<itemtemplate>
here is a value: <%# container.dataitem %>
</itemtemplate>
</asp:datalist>

  在這種情況下,mylist控件的數(shù)據(jù)源是編程設(shè)定的,接著調(diào)用了databind()方法。

  調(diào)用某個控件的databind方法將引發(fā)一個遞規(guī)樹(從該控件開始的到樹中的下層控件);該層次中的每個服務(wù)器控件的databinding事件都會被引發(fā),控件中的數(shù)據(jù)綁定表達(dá)式相應(yīng)地計算出值。因此,如果頁面的databind方法被調(diào)用,那么頁面中的每個數(shù)據(jù)綁定表達(dá)式都會被調(diào)用。

<html>
<script language="vb" runat=server>
sub page_load(sender as object, e as eventargs)
dim items as new arraylist
items.add("one")
items.add("two")
items.add("three")
mylist.datasource = items
mylist.databind()
end sub

</script>
<body>
<asp:datalist id="mylist" runat=server>
<itemtemplate>
here is a value: <%# container.dataitem %>
</itemtemplate>
</asp:datalist>
</body>
</html>

  asp.net 2.0還包含了一種新的簡化的數(shù)據(jù)綁定語法,它允許控件自動地數(shù)據(jù)綁定到數(shù)據(jù)源控件,而無需在頁面代碼中調(diào)用databind()。在“執(zhí)行數(shù)據(jù)訪問”章節(jié)中會討論這種語法。

  對象標(biāo)記語法:<o(jì)bject runat="server" />

  對象標(biāo)記允許頁面開發(fā)者使用宣告式的、基于標(biāo)記的語法來聲明和建立變量實例。下面的例子演示了如何使用對象標(biāo)記來建立arraylist類的實例。

  在運(yùn)行的時候該對象會被自動地建立,并可以通過id“items”訪問它。

<html>
<script language="vb" runat=server>
sub page_load(byval sender as object, byval e as eventargs)
arrayitems.add("one")
arrayitems.add("two")
arrayitems.add("three")
mylist.datasource = arrayitems
mylist.databind()
end sub
</script>

<body>
<o(jì)bject id="arrayitems" class="system.collections.arraylist" runat=server/>
<asp:datalist id="mylist" runat=server>
<itemtemplate>
here is a value: <%# container.dataitem %>
</itemtemplate>
</asp:datalist>
</body>
</html>

  服務(wù)器端注釋語法:<%-- comment --%>

  服務(wù)器端注釋讓頁面開發(fā)者能夠阻止服務(wù)器代碼(包括服務(wù)器控件)和靜態(tài)內(nèi)容的執(zhí)行和呈現(xiàn)。下面的例子演示了如何阻止內(nèi)容的執(zhí)行和發(fā)送給客戶端。請注意,<%--和--%>之間的所有信息都會被過濾掉,并且只有在原始的服務(wù)器文件中才可以看見,即使它包含了其它的asp.net指令。

<html>
<body>
the below content has been hidden from browser clients using a server-side comment
(view the .aspx source to see what we mean :-)
<%--
<asp:calendar id="mycal" runat=server/>
<% for i = 0 to 44 %>
hello world <br>
<% next %>
--%>
</body>
</html>

  服務(wù)器端文件包含語法:<-- #include file="locaton.inc" -->

  服務(wù)器端文件包含(#include)允許開發(fā)者在asp.net頁面的任何位置插入特定文件的內(nèi)容。下面的例子演示了如何在一個頁面中插入自定義的標(biāo)題和腳注。

<html>
<body>
<!-- #include file="header.inc" -->
<br />
<h3> main page content </h3>
<br />
<!-- #include file="footer.inc" -->
</body>
</html>

  表達(dá)式語法:<%$ ... %>2.0中的新特性

  asp.net 2.0增加了一個新的用于在頁面分析之前進(jìn)行值替代的宣告式表達(dá)式語法。當(dāng)我們需要用web.config文件中的連接字符串值或應(yīng)用程序設(shè)置替換服務(wù)器控件屬性值的時候,它就非常有用。在本地化(locaization)的時候,它還可以用于替換資源文件中的值。

<asp:sqldatasource id="sqldatasource1" connectionstring='<%$ connectionstrings:pubs %>' runat="server" selectcommand="sp_getauthors" />
<asp:label id="label1" text='<%$ resources: exchrate, convertlabel %>' runat="server"/>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邓州市| 府谷县| 镇江市| 唐河县| 临湘市| 化德县| 平塘县| 汉阴县| 信丰县| 娱乐| 双流县| 丰宁| 顺昌县| 灵川县| 岳阳市| 鹤山市| 普安县| 浮山县| 武隆县| 白水县| 德阳市| 大港区| 紫云| 扶绥县| 昌宁县| 无为县| 临桂县| 红桥区| 铁岭市| 桃江县| 辽宁省| 石狮市| 崇阳县| 壶关县| 广河县| 梨树县| 仁怀市| 治多县| 静海县| 渝北区| 加查县|