天性懒惰

Give me a try, believe that I can fly!--集歧步以千里,聚沙以成塔。偷懒的最高境界就是更加努力的工作!

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
原文地址:http://www.asp.net/QuickStart/aspnet/doc/pages/syntax.aspx#databind

一个asp.net页面是一个以.aspx文件名后缀向外发布的文本文件。你可以有八种不同的语法标记元素来扩充静态内容。本节QuickStart复习了每种语法元素并提供了它们的使用演示。

呈现代码语法:<% %>和<%= %>

代码呈现的块是用<%…%>来标识的,在网页执行解释的时候允许你自定义呈现内容的执行。下面的例子演示了如何利用它们循环呈现html内容的。

C# VB  
		
<% for (int i=0; i<8; i++) { %>
     <font size="<%=i%>"> Hello World! </font> <br>
<% } %>

VB Reference1.aspx
Run Sample View Source

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="VB" runat=server>
Function Subtract(num1 As Integer, num2 As Integer) As Integer
  Return(num1 - num2)
End Function
</script>

<%
  ...
  number = subtract(number, 1)
  ...
%>

VB Reference2.aspx
Run Sample View Source

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"

VB Reference3.aspx
Run Sample View Source

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="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"/>

VB Reference4.aspx
Run Sample View Source

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();
}

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.

VB Reference5.aspx
Run Sample View Source

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");
  ...
}

VB Reference6.aspx
Run Sample View Source

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


VB Reference7.aspx
Run Sample View Source

Server-Side Include Syntax: <-- #Include File="Locaton.inc" -->


服务器端#Includes使开发者在asp.net页面的任何位置插入制定的文件内容。下面大例子演示了怎样在一个页面中插入一个通用的header和footer
<!-- #Include File="Header.inc" --> ... <!-- #Include File="Footer.inc" -->
VB Reference8.aspx
Run Sample View Source

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

posted on 2006-04-30 01:10  Wigruky  阅读(402)  评论(0)    收藏  举报