📄 syntaxandsemantics.aspx
字号:
<%@ Register TagPrefix="Acme" Namespace="Acme" Assembly="QSTools" %>
<%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/aspplus/util/SrcRef.ascx"%>
<!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
<h4>语法和语义</h4>
ASP.NET 与传统的 ASP 在 API 方面完全兼容,但有以下三处不同:
<p>
<ul>
<li><b>Request()</b>:ASP 返回字符串数组;ASP.NET 返回字符串。
<li><b>Request.QueryString()</b>:ASP 返回字符串数组;ASP.NET 返回字符串。
<li><b>Request.Form()</b>:ASP 返回字符串数组;ASP.NET 返回字符串。
</ul>
<p>
在 ASP 中,<b>Request</b>、<b>Request.QueryString</b> 和 <b>Request.Form</b> 集合从查找返回字符串数组。例如,在传统的 ASP 中,按如下所示访问从请求到 <nobr><u>http://localhost/test/Test.asp?values=45&values=600</u></nobr> 的查询字符串值:
<p>
<div class="code"><pre>
<%
' Below line outputs: "45, 600"
Response.Write Request.QueryString("values")
' Below line outputs: "45"
Response.Write Request.QueryString("values")(1)
%>
</pre></div>
<p>
在 ASP.NET 中,这些集合需要显式方法来获取数组访问。这些数组现在也是从 0 开始索引。例如,在 ASP.NET 中,按如下所示访问从请求到 <nobr><u>http://localhost/test/Test.aspx?values=45&values=600</u></nobr> 的查询字符串值:
<p>
<Acme:TabControl runat="server">
<Tab Name="C#">
<%
// Below line outputs: "45, 600"
Response.Write(Request.QueryString["values"]);
// Below line outputs: "45"
Response.Write(Request.QueryString.GetValues("values")[0]);
%>
</Tab>
<Tab Name="VB">
<%
' Below line outputs: "45, 600"
Response.Write(Request.QueryString("values"))
' Below line outputs: "45"
Response.Write(Request.QueryString.GetValues("values")(0))
%>
</Tab>
<Tab Name="JScript">
<%
// Below line outputs: "45, 600"
Response.Write(Request.QueryString["values"]);
// Below line outputs: "45"
Response.Write(Request.QueryString.GetValues("values")[0]);
%>
</Tab>
</Acme:TabControl>
<p>
这些数组最常用于从多项选择列表框 (<select multiple>) 传递窗体值或多个复选框具有相同名称的情况。
<p>
<h5>ASP.NET 和 ASP 之间的语义差异</h5>
ASP.NET 页与现有的 ASP 页相比还有几处语义变化。下列问题最有可能影响您:
<p>
<ul>
<li><b>ASP.NET 页仅支持单语言。</b>
<p>
ASP 允许在单页上使用多种语言,这对脚本库方案有用。由于 ASP.NET 的已编译特性,它在一页上仅支持单语言。然而,在单个应用程序内仍然可以有多个使用不同语言的页。用户控件还可以具有不同于包含它们的页所使用的语言。这使您能够在单页内集成用不同语言编写的功能。这足以替代传统 ASP 应用程序中普遍使用的多语言包含文件。
<p>
<li><b>ASP.NET 页函数必须在 <script runat=server> 块中声明。</b>
<p>
在 ASP 中,页函数可以在 <% %> 块中声明:
<div class="code"><pre>
<%
Sub DoSomething()
Response.Write "Hello World!"
End Sub
DoSomething
%>
</pre></div>
<p>
在 ASP.NET 中,页函数必须在 <script runat=server> 块中声明:
<p>
<table cellpadding=0 cellspacing=0 width="93%">
<tr>
<td>
<Acme:TabControl runat="server">
<Tab Name="C#">
<script language="C#" runat=server>
void DoSomething() {
Response.Write("Hello World!");
}
</script>
<%
DoSomething();
%>
</Tab>
<Tab Name="VB">
<script language="VB" runat=server>
Sub DoSomething()
Response.Write ("Hello World!")
End Sub
</script>
<%
DoSomething()
%>
</Tab>
<Tab Name="JScript">
<script language="JScript" runat=server>
function DoSomething() : void {
Response.Write("Hello World!");
}
</script>
<%
DoSomething();
%>
</Tab>
</Acme:TabControl>
</td>
</tr>
</table>
<p>
<li><b>ASP.NET 不支持页呈现函数。</b>
<p>
在 ASP 中,可以用 <% %> 块声明页呈现函数:
<p>
<div class="code"><pre>
<% Sub RenderSomething() %>
<font color="red"> Here is the time: <%=Now %> </font>
<% End Sub %>
<%
RenderSomething
RenderSomething
%>
</pre></div>
<p>
在 ASP.NET 中,这必须重写:
<p>
<table cellpadding=0 cellspacing=0 width="93%">
<tr>
<td>
<Acme:TabControl runat="server">
<Tab Name="C#">
<script language="C#" runat=server>
void RenderSomething() {
Response.Write("<font color=red> ");
Response.Write("Here is the time: " + DateTime.Now);
}
</script>
<%
RenderSomething();
RenderSomething();
%>
</Tab>
<Tab Name="VB">
<script language="VB" runat=server>
Sub RenderSomething()
Response.Write("<font color=red> ")
Response.Write("Here is the time: " & Now)
End Sub
</script>
<%
RenderSomething()
RenderSomething()
%>
</Tab>
<Tab Name="JScript">
<script language="JScript" runat=server>
function RenderSomething() : void {
Response.Write("<font color=red> ");
Response.Write("Here is the time: " + DateTime.Now);
}
</script>
<%
RenderSomething();
RenderSomething();
%>
</Tab>
</Acme:TabControl>
</td>
</tr>
</table>
</ul>
<h4><a name="summary">本节小结</a></h4>
<ol>
<li>除了三处例外,ASP.NET 与传统的 ASP 在 API 方面完全兼容。API 的变化是:<b>Request()</b>、<b>Request.QueryString()</b> 和 <b>Request.Form()</b> 现在都返回个别的字符串而不是字符串数组。
<li>ASP.NET 页仅支持单语言。
<li>ASP.NET 页函数必须在 <script runat=server> 块中声明。
<li>不支持页呈现函数。
</ol>
<p>
<!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -