一个asp.net页面是一个以.aspx文件名后缀向外发布的文本文件。你可以有八种不同的语法标记元素来扩充静态内容。本节QuickStart复习了每种语法元素并提供了它们的使用演示。
呈现代码语法:<% %>和<%= %>
代码呈现的块是用<%…%>来标识的,在网页执行解释的时候允许你自定义呈现内容的执行。下面的例子演示了如何利用它们循环呈现html内容的。
C# | VB | |
<% for (int i=0; i<8; i++) { %> <font size="<%=i%>"> Hello World! </font> <br> <% } %> <% For I=0 To 7 %> <font size="<%=i%>"> Hello World! </font> <br> <% Next %> |
Code enclosed by <% ... %> is just executed, while expressions that include an equal sign, <%= ... %>, are evaluated and the result is emitted as content. Therefore
<%="Hello World" %>
renders the same thing as the C# code
<% Response.Write("Hello World"); %>
.
Note: For languages that use marks to end or separate statements (for example, the semicolon (;) in C#), it is important to place those marks correctly depending on how your code should be rendered.
C# code | |
---|---|
<% Response.Write("Hello World"); %> | A semicolon is necessary to end the statement. |
<%="Hello World"; %> | Wrong: Would result in "Response.Write("Hello World";); ". |
<%="Hello World" %> | A semicolon is not necessary. |
Declaration Code Syntax: <script runat="server">
Code declaration blocks define member variables and methods that will be compiled into the generated Page class. These blocks can be used to author page and navigation logic. The following example demonstrates how a Subtract method can be declared within a <script runat="server"> block, and then invoked from the page.C# | VB | |
<script language="C#" runat=server> int subtract(int num1, int num2) { return num1 - num2; } </script> <% ... number = subtract(number, 1); ... %> <script language="VB" runat=server> Function Subtract(num1 As Integer, num2 As Integer) As Integer Return(num1 - num2) End Function </script> <% ... number = subtract(number, 1) ... %> |
Important: Unlike ASP -- where functions could be declared within <% %> blocks -- all functions and global page variables must be declared in a <script runat=server> tag. Functions declared within <% %> blocks will now generate a syntax compile error.
Server Control Syntax
Custom ASP.NET server controls enable page developers to dynamically generate HTML user interface (UI) and respond to client requests. They are represented within a file using a declarative, tag-based syntax. These tags are distinguished from other tags because they contain a "runat=server" attribute. The following example demonstrates how an <asp:label runat="server"> server control can be used within an ASP.NET page. This control corresponds to the Label class in the System.Web.UI.WebControls namespace, which is included by default.By adding a tag with the ID "Message", an instance of Label is created at run time:
<asp:label id="Message" font-size=24 runat="server"/>The control can then be accessed using the same name. The following line sets the Text property of the control.
C# | VB | |
Message.Text = "Welcome to ASP.NET"; Message.Text = "Welcome to ASP.NET" |
HTML Server Control Syntax
HTML server controls enable page developers to programmatically manipulate HTML elements within a page. An HTML server control tag is distinguished from client HTML elements by means of a "runat=server" attribute. The following example demonstrates how an HTML <span runat=server> server control can be used within an ASP.NET page.As with other server controls, the methods and properties are accessible programmatically, as shown in the following example.
C# | VB | |
<script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e) { Message.InnerHtml = "Welcome to ASP.NET"; } </script> ... <span id="Message" style="font-size:24" runat="server"/> <script language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Message.InnerHtml = "Welcome to ASP.NET" End Sub </script> ... <span id="Message" style="font-size:24" runat="server"/> |
Data Binding Syntax: <%# %>
在asp.net中绑定数据的创建使开发者能够分级的向数据容器中绑定值。在<%#%>中定位的绑定代码只有在它所在容器执行数据绑定方法时执行。下面的例子演示了在datalist服务器控件datalist中怎样使用数据绑定语法。
Within the datalist, the template for one item is specified. The content of the item template is specified using a data binding expression and the
Container.DataItem
refers to the data source used by the datalist
MyList
.
<asp:datalist id="MyList" runat=server> <ItemTemplate> Here is a value: <%# Container.DataItem %> </ItemTemplate> </asp:datalist>In this case the data source of the
MyList
control is set programmatically, and then
DataBind()
is called.
C# | VB | |
void Page_Load(Object sender, EventArgs e) { ArrayList items = new ArrayList(); items.Add("One"); items.Add("Two"); items.Add("Three"); MyList.DataSource = items; MyList.DataBind(); } 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 |
Calling the DataBind method of a control causes a recursive tree walk from that control on down in the tree; the DataBinding event is raised on each server control in that hierarchy, and data binding expressions on the control are evaluated accordingly. So, if the DataBind method of the page is called, then every data binding expression within the page will be called.
ASP.NET 2.0 also includes a new simplified databinding syntax, that allows controls to automatically data-bind to data source controls, without requiring you to call DataBind() in page code. This syntax is discussed in the section on Performing Data Access.
Object Tag Syntax: <object runat="server" />
对象标签(Object Tag)允许开发者用一个基于标签的语法来声明并创建一个变量的实例。下面的实例演示了Object Tag
怎样用来创建一个ArrayList类的实例。
<object id="items" class="System.Collections.ArrayList" runat="server"/>
对象将会在运行时自动的创建,并通过"items"的ID号来处理。
C# | VB | |
void Page_Load(Object sender, EventArgs e) { items.Add("One"); items.Add("Two"); items.Add("Three"); ... } Sub Page_Load(sender As Object, e As EventArgs) items.Add("One") items.Add("Two") items.Add("Three") ... End Sub |
Server-Side Comment Syntax: <%-- Comment --%>
服务器端声明能使开发者来阻止从服务器端运行和呈现服务器端代码(包括服务器端控件)和静态内容。下面的实例演示了怎么样来阻止内容执行到客户端。注意在<%--and --%>中的任何代码都被过滤掉,仅仅在出现的原始的服务器端文件中,甚至可是用来包括另外的asp.net代码。C# | VB | |
<%-- <asp:calendar id="MyCal" runat=server/> <% for (int i=0; i<45; i++) { %> Hello World <br> <% } %> --%> <%-- <asp:calendar id="MyCal" runat=server/> <% For I=0 To 44 %> Hello World <br> <% Next %> --%> |
Server-Side Include Syntax: <-- #Include File="Locaton.inc" -->
服务器端#Includes使开发者在asp.net页面的任何位置插入制定的文件内容。下面大例子演示了怎样在一个页面中插入一个通用的header和footer
<!-- #Include File="Header.inc" --> ... <!-- #Include File="Footer.inc" -->
Expression Syntax: <%$ ... %> New in 2.0
asp.net2.0添加了一个新的在页面被解析之前在页面中取代值的表达式语法。它在取代定义在服务器端web.config中的连接字符串或应用程序设置时时非常有用的 。
它也能用在取代本地资源文件中的值。更多的关于连接字符串、资源表达式和表达式控制的内容请参考Performing Data Access, Internationalizing Your Application
和Extending ASP.NET小结的内容。
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString='<%$ connectionStrings:Pubs %>' runat="server" SelectCommand="sp_GetAuthors" /> <asp:Label ID="Label1" Text='<%$ Resources: ExchRate, ConvertLabel %>' runat="server"/>