Building a Web Application->Web Forms Syntax Reference
An ASP.NET Web Forms page is a declarative//描述性的 text file //文本文件with an .aspx file name extension//文件扩展名. In addition to static content//除了静态的内容之外, you can use eight distinct //独特的syntax markup elements//语法标记元素. This section//本部分 of the QuickStart reviews //检视each of these syntax elements //语法元素and provides examples demonstrating//举例说明 their use//他们的用法.
Code enclosed //封闭by <% ... %> is just executed, while expressions//表达式 that include an equal sign//等号, <%= ... %>, are evaluated //被估价and the result//结果 is emitted //发布as content//作为内容. Therefore//所以
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//描述,写.
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//产生语法编译错误.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:
As with //与....一样other server controls, the methods and properties are accessible programmatically//计划地, as shown in the following example.
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
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.
This header has been included using a server-side include
.
This footer has been included using a server-side include
.
Rendering Code Syntax: <% %> and <%= %>
Code rendering blocks//代码表示块 are denoted //指示,标记with <% ... %> elements, allow you to custom-control content emission//发射,射出,发行, and execute during the render phase //在他们表现阶段of Web Forms page execution//网站表单页面执行.<注:这句话没怎么懂> The following example demonstrates how you can use them to loop over HTML content.| C# | VB | |
<% for (int i=0; i<8; i++) { %>
<font size="<%=i%>"> Hello World! </font> <br>
<% } %>
|
||
C# Reference1.aspx
<%@ Page Language="C#" %>

<html>

<body>

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

</body>

</html>
<%@ Page Language="C#" %>
<html>
<body>
<% for (int i=0; i<8; i++) { %>
<font size="<%=i%>"> Hello World! </font> <br>
<% } %>
</body>
</html>
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);
...
%>
|
||
C# Reference2.aspx
<html>

<script language="C#" runat=server>

int subtract(int num1, int num2) {
return num1-num2;
}

</script>

<body>
<%
int number = 100;

while (number > 0) {
Response.Write("Value: " + number + "<br>");
number = subtract(number, 1);
}
%>

</body>

</html>
<html>
<script language="C#" runat=server>
int subtract(int num1, int num2) {
return num1-num2;
}
</script>
<body>
<%
int number = 100;
while (number > 0) {
Response.Write("Value: " + number + "<br>");
number = subtract(number, 1);
}
%>
</body>
</html>
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 theBy 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";
|
||
C# Reference3.aspx
<html>

<script language="C#" runat=server>

void Page_Load(Object sender, EventArgs e) {
Message.Text = "Welcome to ASP.NET";
}

</script>

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

</body>

</html>
<html>
<script language="C#" runat=server>
void Page_Load(Object sender, EventArgs e) {
Message.Text = "Welcome to ASP.NET";
}
</script>
<body>
<asp:label id="Message" font-size=24 runat=server/>
</body>
</html>
HTML Server Control Syntax //HTML服务器端控件语法
HTML server controls enable page developers to programmatically//计划地 manipulate HTML elements//html元素 within a page. An HTML server control tag is distinguished//区别开来 from client HTML elements //客户端HTML元素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"/>
|
||
C# Reference4.aspx
<html>

<script language="C#" runat=server>

void Page_Load(Object sender, EventArgs e) {
Message.InnerHtml = "Welcome to ASP.NET";
}

</script>

<body>
<span id="Message" style="font-size:24" runat=server/>

</body>

</html>
<html>
<script language="C#" runat=server>
void Page_Load(Object sender, EventArgs e) {
Message.InnerHtml = "Welcome to ASP.NET";
}
</script>
<body>
<span id="Message" style="font-size:24" runat=server/>
</body>
</html>
Data Binding Syntax: //数据绑定的语法<%# %>
The data binding support built into ASP.NET enables page developers to hierarchically //体系的,分层的bind control properties to data container//数据容器 values. Code located within a <%# %> code block is only executed when the DataBind method of its parent control container is invoked. The following example demonstrates how to use the data binding syntax within an <asp:datalist runat=server> control.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//数据列表名为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.
C# Reference5.aspx
<html>

<script language="C#" runat=server>

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

</script>

<body>

<asp:datalist id="MyList" runat=server>

<ItemTemplate>

Here is a value: <%# Container.DataItem %>

</ItemTemplate>

</asp:datalist>

</body>

</html>
<html>
<script language="C#" runat=server>
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();
}
</script>
<body>
<asp:datalist id="MyList" runat=server>
<ItemTemplate>
Here is a value: <%# Container.DataItem %>
</ItemTemplate>
</asp:datalist>
</body>
</html>
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 tags enable page developers to declare and create instances of variables using a declarative, tag-based syntax. The following example demonstrates how the object tag can be used to create an instance of an ArrayList class.<object id="items" class="System.Collections.ArrayList" runat="server"/>The object will be created automatically at run time and can then be accessed through the ID "items".
| C# | VB | |
void Page_Load(Object sender, EventArgs e) {
items.Add("One");
items.Add("Two");
items.Add("Three");
...
}
|
||
C# Reference6.aspx
<html>

<script language="C#" runat=server>

void Page_Load(Object sender, EventArgs e) {

ArrayItems.Add("One");
ArrayItems.Add("Two");
ArrayItems.Add("Three");

MyList.DataSource = ArrayItems;
MyList.DataBind();
}

</script>

<body>
<object 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>
<html>
<script language="C#" runat=server>
void Page_Load(Object sender, EventArgs e) {
ArrayItems.Add("One");
ArrayItems.Add("Two");
ArrayItems.Add("Three");
MyList.DataSource = ArrayItems;
MyList.DataBind();
}
</script>
<body>
<object 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>
Server-Side Comment Syntax: //服务器端注释语法<%-- Comment --%>
Server-side comments enable page developers to prevent server code (including server controls) and static content from executing or rendering//阻止服务器端代码(包括服务器端控件)和静态内容执行或者显示. The following sample demonstrates how to block content from executing and being sent down to a client//发送给客户端. Note that everything between <%-- and --%> is filtered out //过滤出and only visible//可见的 in the original server file//原始的服务端文件, even though//甚至尽管 it contains other ASP.NET directives它包含了其他的ASP.NET的指令.| C# | VB | |
<%--
<asp:calendar id="MyCal" runat=server/>
<% for (int i=0; i<45; i++) { %>
Hello World <br>
<% } %>
--%>
|
||
C# Reference7.aspx
<%@ Page Language="C#"%>
<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 (int i=0; i<45; i++) { %>
Hello World <br>
<% } %>

--%>

</body>

</html>
<%@ Page Language="C#"%>
<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 (int i=0; i<45; i++) { %>
Hello World <br>
<% } %>
--%>
</body>
</html>
Server-Side Include Syntax: //服务器端的包含语法<-- #Include File="Locaton.inc" -->
Server-side #Includes enable developers to insert the raw //生的,外来的contents of a specified file //指定的文件anywhere within an ASP.NET page. The following sample demonstrates how to insert a custom header and footer within a page.<!-- #Include File="Header.inc" --> ... <!-- #Include File="Footer.inc" -->
C# Reference8.aspx
文件夹层次结构如下:

其中:reference8_cs.aspx
文件夹层次结构如下:
其中:reference8_cs.aspx
<%@ Page Language="C#"%>
<html>

<body>

<!-- #Include File="Header.inc" -->
<br/>

<h3> Main page content </h3>

<br/>

<!-- #Include File="Footer.inc" -->

</body>

</html>
<%@ Page Language="C#"%>
<html>
<body>
<!-- #Include File="Header.inc" -->
<br/>
<h3> Main page content </h3>
<br/>
<!-- #Include File="Footer.inc" -->
</body>
</html>
Header.inc
This header has been included using a server-side include
.
Footer.inc
This footer has been included using a server-side include
.
Expression Syntax: //表达式语法<%$ ... %> New in 2.0
ASP.NET 2.0 adds a new declarative //声明的,描述性的expression syntax for substituting //代替values into a page before the page is parsed//解析,从语法上分析. This is useful for substituting //代替connection string values or application settings //应用程序设置defined in a Web.config file for server control property values. It can also be used to substitute//代替 values from a resource file//源程序文件 for localization//本地化,地方化. More on connection string and resources expressions and expression handlers can be found in the Performing Data Access//下一节会讨论此部分内容, Internationalizing//国际化 Your Application //你的应用程序and Extending ASP.NET sections. //部分,剖面,截面<asp:SqlDataSource ID="SqlDataSource1" ConnectionString='<%$ connectionStrings:Pubs %>' runat="server" SelectCommand="sp_GetAuthors" /> <asp:Label ID="Label1" Text='<%$ Resources: ExchRate, ConvertLabel %>' runat="server"/>
浙公网安备 33010602011771号