asp+语法介绍

来源:互联网 发布:八大菜系之首知乎 编辑:程序博客网 时间:2024/04/29 12:25

*/ASP+ 现在支持两种语言C# (简称 "C Sharp"), Visual Basic, and Jscript.
基于习惯,在以下的语言介绍中,我们采用的练习和例程采用VB和C#语言来开发Web应用程序.如果想要得到关于.Net技术的详细资
料,请去MS的站点 查看关于 NGWS SDK!
在下面的列表中,你可以看到关于这两种语言的语法的简要介绍
1.变量声名
C# 语法
int x;
String s;
String s1, s2;
Object o;
Object obj = new Object();
public String name;
VB语法
Dim x As Integer
Dim s As String
Dim s1, s2 As String
Dim o 'Implicitly Object
Dim obj As New Object()
Public name As String


2语句
C#:
Response.Write("豆腐");
VB:
Response.Write("豆腐")

3.注释语句
//豆腐制作,都是精品
/*
豆腐制作

都是精品
*/

VB:
'豆腐制作,都是精品
' 豆腐制作
',
'都是精品
4.获得URL 传递的变量
C#:
String s = Request.QueryString["Name"];
String value = Request.Cookies["key"];
VB:
Dim s, value As String
s = Request.QueryString("Name")
value = Request.Cookies("Key").value  
5.声明属性
C#:
public String name {

  get {
    ...
    return ...;
  }

  set {
    ... = value;
  }

}

VB:
Public Property Name As String

  Get
    ...
    Return ...;
  End Get

  Set
    ... = value;
  End Set

End Property
6.数组
C#
    String[] a = new String[3];
    a[0] = "1";
    a[1] = "2";
    a[2] = "3";
    //二维数组
    String[][] a = new String[3][3];
    a[0][0] = "1";
    a[1][0] = "2";
    a[2][0] = "3";
VB:
  Dim a(3) As String
  a(0) = "1"
  a(1) = "2"
  a(2) = "3"

  Dim a(3,3) As String
  a(0,0) = "1"
  a(1,0) = "2"
  a(2,0) = "3"

  Dim a() As String
  a(0,0) = "1"
  a(1,0) = "2"
  a(2,0) = "3"

  Dim a(,) As String
  a(0,0) = "1"
  a(1,0) = "2"
  a(2,0) = "3"


7变量初始化
C#:
String s = "Hello World";
int i = 1
double[] a =  { 3.00, 4.00, 5.00 };
VB:
Dim s As String = "Hello World"
Dim i As Integer = 1
Dim a() As Double = { 3.00, 4.00, 5.00 }

8;判断语句(If 语句)
if (Request.QueryString != null) {
  ...
}

VB:
If Not (Request.QueryString = Null)
  ...
End If

9.分支语句(case 语句)
C#:
switch (FirstName) {
  case "John" :
    ...
    break;
  case "Paul" :
    ...
    break;
  case "Ringo" :
    ...
    break;
}
VB:
Select (FirstName)
  case "John" :
    ...
  case "Paul" :
    ...
  case "Ringo" :
    ...
End Select

10 For循环语句
C#
for (int i=0; i<3; i++)
  a(i) = "test";
VB:
  Dim I As Integer
  For I = 0 To 2
    a(I) = "test"
  Next

11 While 循环
C#:
int i = 0;
while (i<3) {
  Console.WriteLine(i.ToString());
  i += 1;
}
VB:
Dim I As Integer
I = 0
Do While I < 3
  Console.WriteLine(I.ToString())
  I = I + 1
Loop
12 字符串连接
C#:
String s1;
String s2 = "hello";
s2 += " world";
s1 = s2 + " !!!";
VB:
Dim s1, s2 As String
s2 = "hello"
s2 &= " world"
s1 = s2 & " !!!"


声明事件
C#:
void MyButton_Click(Object sender,
                            EventArgs E) {
...
}
VB:
Sub MyButton_Click(Sender As Object,
                            E As EventArgs)
...
End Sub


13 声明Object
C#
MyObject obj = (MyObject)Session["Some value"];
IMyObject iObj = obj
VB:
Dim bj As MyObject
Dim iObj As IMyObject
obj = Session("Some value")
iObj = CType(obj, IMyObject)


14 数据类型转换
C#
int i = 3;
String s = i.ToString();
double d = Double.Parse(s);
VB:
Dim i As Integer
Dim s As String
Dim d As Double

i = 3
s = i.ToString()
d = CDbl(s)


15 类的声明和继承
C#:
using System;

namespace MySpace {

  public class Foo : Bar {

    int x;

    public Foo() { x = 4; }
    public void Add(int x) { this.x += x; }   
    public int GetNum() { return x; }   
  }

}

VB:
Imports System

Namespace MySpace

  Public Class Foo : Inherits Bar

    Dim x As Integer

    Public Sub New()
      MyBase.New()
      x = 4
    End Sub

    Public Sub Add(x As Integer)
      Me.x = Me.x + x
    End Sub

    Public Function GetNum() As Integer
       Return x
    End Function

  End Class

End Namespace

16 声明类的主函数
C#:
using System;

public class ConsoleCS {

  public ConsoleCS() {
    Console.WriteLine("Object Created");
  }

  public static void Main (String[] args) {
    Console.WriteLine("Hello World");
    ConsoleCS ccs = new ConsoleCS();
  }

}

VB
Imports System

Public Class ConsoleVB

  Public Sub New()
    MyBase.New()
    Console.WriteLine("Object Created")
  End Sub

  Public Shared Sub Main()
    Console.WriteLine("Hello World")
    Dim cvb As ConsoleVB
    cvb = New ConsoleVB()
  End Sub

End Class


17 标准模块
C#
using System;

public class Module {

public static void Main (String[] args) {
  Console.WriteLine("Hello World");
}

}
VB:
Imports System

Public Module ConsoleVB

  Public Sub Main()
    Console.WriteLine("Hello World")
  End Sub

End Module

本篇文章是由一篇英语的文章翻译来的,从这里面我们可以看到MS 为了统治Web编程领域,花费了多大的心思!
他完全的重新定义了Web编程的全部规范,使得Web编程变的更加简单和功能强大!
现在在MS 的站点上已经可以 下载 asp+ 的解释器,但是太大!豆腐没有下载,哪位朋友有这个能力,下载下来,一读为快!
顺便给大家介绍一个学习 Asp+ 的比较好的站点!只可惜 目前只有  英文的!我会在 适当 的时间里,给大家  翻译  尽可能  
多的文章!
站点的 URl是:
http://tutorial.superexpert.com/quickstart/aspplus/doc/langsupport.aspx
还有一个
http://www.15seconds.com也有关于 Asp+ 的文章

asp+ 页面的文件和asp 一样,也是一个 文本的文件,但是他的后缀名称已经不再是 .asp 而是 .asp+
当客户端浏览器向 IIS 发出.aspx 的文件请求后,IIS 会 首先将.aspx文件编译成运行状态的NGWS 类文件来运行,请注意,这个
编译的过程只在第一次运行的时候发生,以后就直接以运行态的NGWS 类运行了(和 .jsp 是不是很类似??--豆腐添加,原文没
有)

一个 最简单 Asp+ 文件可以通过将 一个 html  文件的后缀名称修改为.aspx 来生成!在下面的例子中我们将作一个这样的例子
运行的范例请看这里:
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro1.aspx
原代码如下:
  <html>
   <head>
      <link rel="stylesheet"href="intro.css">
   </head>

   <body>



       <center>

       <form action="intro1.aspx" method="post">

           <h3> Name: <input id="Name" type=text>

           Category:  <select id="Category" size=1>
                          <option>psychology</option>
                          <option>business</option>
                          <option>popular_comp</option>
                      </select>

           <input type=submit value="Lookup">

       </form>

       </center>

   </body>
</html>
(豆腐添加:
有的人会说,这个例子太简单了或者说根本就不是一个例子,但是对于学习来说,最起码让我们可以更深入的了解一下 asp+ 的一
些神秘的外表,下面我们将要讲解一个 带有<%%>标签的粒子)
asp+文件和asp文件是兼容的,在<%%>之间我们可以使用嵌套的HTML语言,下面就是一个很简单的 和 asp 文件完全兼容 asp+ 文

<html>
   <head>
      <link rel="stylesheet"href="intro.css">
   </head>

   <body>

       <center>

       <form action="intro2.aspx" method="post">

           <h3> Name: <input id="Name" type=text>

           Category:  <select id="Category" size=1>
                          <option>psychology</option>
                          <option>business</option>
                          <option>popular_comp</option>
                      </select>

           <input type=submit value="Lookup">

           <p>
         
           <% for i=0 to 7 %>
              <font size="<%=i%>"> Welcome to ASP+ </font> <br>
           <% next %>

       </form>

       </center>

   </body>
</html>
这个例子的运行请看
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro2.aspx
(豆腐添加:上面这个例子演示了aspx文件和asp文件的完全兼容性,但是仅仅是这样,aspx不会成为一个新的热点,下面会简单介
绍下aspx文件的一个新创的功能)
提示:和asp不同的是,在<%%>中包含的代码,是被编译执行的,而不是象asp 一样是脚本级的执行

asp+ 文件中的 <% %> 代码可以和 asp 一样动态的去修改 HTML 的输出显示使得 客户端的 内容有所改变
<%@ Page Language="VB" %>

<html>
   <head>
      <link rel="stylesheet"href="intro.css">
   </head>

   <body>

       <center>

       <form action="intro3.aspx">

           <h3> Name: <input name="Name" type=text value="<%=Request.QueryString("Name")%>">

           Category:  <select name="Category" size=1>
                         
                         <%
                             Dim I As Integer
                             Dim values(3) As String
                             values(0) = "psychology"
                             values(1) = "business"
                             values(2) = "popular_comp"

                             For I = 0 To values.Length - 1
                          %>

                                <% If (Request.QueryString("Category") = values(i)) %>
                                  <option selected>
                                <% Else %>
                                  <option>
                                <% End If %>
                                   <%=values(i)%>
                                </option>

                          <% Next %>

                      </select>

           <input type=submit name="Lookup" value="Lookup">

           <p>

           <% If (Not Request.QueryString("Lookup") = Null) %>

              Hi <%=Request.QueryString("Name") %>, you selected: <%=Request.QueryString("Category") %>

           <% End If %>

       </form>

       </center>

   </body>
</html>



运行的例子在
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro4.aspx

这一章介绍关于Asp+的服务器端的控件
除了使用<%%>号以外,asp+ 的程序开发者目前可以使用新的标签来生成动态的页面了,新的服务器控可以在asp+ 文件中利用一个
特殊的tag runat=server来声明
下面的例子中用到了以下几个服务器控件<form runat=server>, <asp:textbox runat=server>, <asp:dropdownlist
runat=server>, and <asp:button runat=server>在运行的过程中他们都会自动生成HTML代码
<html>
   <head>
      <link rel="stylesheet"href="intro.css">
   </head>

   <body>

       <center>

       <form action="intro4.aspx" method="post" runat=server>

           <h3> Name: <asp:textbox id="Name" runat="server"/>

           Category:  <asp:dropdownlist id="Category" runat=server>
                         <asp:listitem>psychology</asp:listitem>
                         <asp:listitem>business</asp:listitem>
                         <asp:listitem>popular_comp</asp:listitem>
                      </asp:dropdownlist>

           <asp:button text="Lookup" runat="server"/>

       </form>

       </center>

   </body>
</html>
这个例子的运行结果在
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro4.aspx

注意:这些服务器控件都会在客户端生成HTML代码,但是这些服务器控件的内容并没有保存在Hidden 中,而是事实在在 的保存
在 页面之间,而且在客户端没有任何的 script 代码

除了这些输入的服务器控件,Asp+ 允许开发者自己去丰富一些定植的控件,例如在下面的例子中我们将要看到的
<asp:adrotator>控件就是动态的生成广告图片

<html>
   <head>
      <link rel="stylesheet"href="intro.css">
   </head>

   <body>

       <center>

       <form action="intro5.aspx" method="post" runat="server">

           <asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>

           <h3> Name: <asp:textbox id="Name" runat="server"/>

           Category:  <asp:dropdownlist id="Category" runat=server>
                         <asp:listitem>psychology</asp:listitem>
                         <asp:listitem>business</asp:listitem>
                         <asp:listitem>popular_comp</asp:listitem>
                      </asp:dropdownlist>

           <asp:button text="Lookup" runat="server"/>

       </form>

       </center>

   </body>
</html>
广告文件的内容是:
<Advertisements>

   <Ad>
      <ImageUrl>/quickstart/aspplus/images/banner1.gif</ImageUrl>
      <TargetUrl>http://www.microsoft.com</TargetUrl>
      <AlternateText>Alt Text</AlternateText>
      <Keyword>Computers</Keyword>
      <Impressions>80</Impressions>
   </Ad>

   <Ad>
      <ImageUrl>/quickstart/aspplus/images/banner2.gif</ImageUrl>
      <TargetUrl>http://www.microsoft.com</TargetUrl>
      <AlternateText>Alt Text</AlternateText>
      <Keyword>Computers</Keyword>
      <Impressions>80</Impressions>
   </Ad>

   <Ad>
      <ImageUrl>/quickstart/aspplus/images/banner3.gif</ImageUrl>
      <TargetUrl>http://www.microsoft.com</TargetUrl>
      <AlternateText>Alt Text</AlternateText>
      <Keyword>Computers</Keyword>
      <Impressions>80</Impressions>
   </Ad>

</Advertisements>

操作服务器控件的事件
每一个asp+的服务器控件都有自己的属性,方法和事件。asp+的开发者现在可以清楚的修改和交互自己的页面
下面的这个例子,我们用到了两个服务器控件<asp:button runat=server> ,<asp:label runat=server>
和button 的 click 事件
<html>
   <head>
      <link rel="stylesheet"href="intro.css">
   </head>

   <script language="VB" runat=server>

       Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
           Message.Text = "Hi " & Name.Text & ", you selected: " & Category.SelectedItem.Text
       End Sub

   </script>

   <body>

       <center>

       <form action="intro6.aspx" method="post" runat="server">

           <asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>

           <h3> Name: <asp:textbox id="Name" runat="server"/>

           Category:  <asp:dropdownlist id="Category" runat=server>
                         <asp:listitem>psychology</asp:listitem>
                         <asp:listitem>business</asp:listitem>
                         <asp:listitem>popular_comp</asp:listitem>
                      </asp:dropdownlist>

           <asp:button type=submit text="Lookup" onClick="SubmitBtn_Click" runat="server"/>

           <p>

           <asp:label id="Message" runat="server"/>

       </form>

       </center>

   </body>
</html>

使用定制的服务器控件
在asp+中提供了45个已经做好了的服务器控件,我们可以将他们象黑盒子一样的使用。除此以外,开发者还可以使用任何第三方开
发的服务器控件
在下面的例子中,我们要用到一个通过<acme:calendar runat=server>标签声明的组件,请注意,在文件的第一行必须使用<%
Register %> 来声明 "Acme Xml "标签的的前缀"Acme".在asp+ 文件中,将会使用这个命名的服务器控件的类的实例。
代码:(由于输入的限制,没有办法了,详细内容可以去 http://www.aspcool.com)
这个例子的演示在
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro7.aspx  
这个日历的服务器控件可以同时支持高级和低级的浏览器,对于高级的浏览器,他在客户端生成DHTML 代码,DHTML 文档不需要和
服务器进行交互!对于低级的浏览器,则生成标准的 HTML 3.2 的代码,这个时候需要和服务器的交互来处理浏览器客户端的用户
操作
注意:这个页面的代码的开发者在书写的过程中,开发者不需要关心客户浏览器是高级的还是低级的。日历控件自己会处理两种浏
览器之间的差别
下一讲,我们将要进入asp+ 操作 数据库 的章节,请大家随时留意我们的站点更新!

asp+ 给我们提供了一套数据表格和数据列表的控件。这些控件可以帮助我们定制我们UI(user interFace 用户界面)而不去考
虑一种数据库或者其他的数据库。例如:在下面的例子中,我们将要介绍一下<asp:datagrid runat=server>控件是怎么样通过
sql 语句给我们提供数据的
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>

<html>
   <head>
      <link rel="stylesheet"href="intro.css">
   </head>

   <script language="VB" runat=server>

       Sub SubmitBtn_Click(Sender As Object, E As EventArgs)

          Dim DS As DataSet
          Dim MyConnection As SQLConnection
          Dim MyCommand As SQLDataSetCommand
          '以下是数据库联结
          MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
          MyCommand = New SQLDataSetCommand("select * from Titles where type='" +
Category.SelectedItem.value + "'", myConnection)

          DS = new DataSet()
          MyCommand.FillDataSet(DS, "Titles")

          MyList.DataSource = DS.Tables("Titles").DefaultView
          MyList.DataBind()

       End Sub


   </script>

   <body>

       <center>

       <form action="intro75.aspx" method="post" runat="server">

           <asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>

           <h3> Name: <asp:textbox id="Name" runat="server"/>

           Category:  <asp:dropdownlist id="Category" runat=server>
                         <asp:listitem >psychology</asp:listitem>
                         <asp:listitem >business</asp:listitem>
                         <asp:listitem >popular_comp</asp:listitem>
                      </asp:dropdownlist>

           <asp:button type=submit text="Lookup" onClick="SubmitBtn_Click" runat="server"/>

           <p>

           <ASP:DataGrid id="MyList" HeaderStyle-BackColor="#aaaadd" BackColor="#ccccff" runat="server"/>

       </form>

       </center>

   </body>

</html>
这个例子的运行示例在
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro75.aspx

数据表格(data grid)控件 <asp:datagrid runat=server>
给我们提供了一种非常简单的方法用传统的UI截面去显示数据查询的结果.Asp+ 的开发者现在还可以通过<asp:dataList
runat=server>
来定制数据列表显示来定制信息

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>

<html>
   <head>
      <link rel="stylesheet"href="intro.css">
   </head>

   <script language="VB" runat=server>

       Sub SubmitBtn_Click(Sender As Object, E As EventArgs)

          Dim DS As DataSet
          Dim MyConnection As SQLConnection
          Dim MyCommand As SQLDataSetCommand

          MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
          MyCommand = New SQLDataSetCommand("select * from Titles where type='" +
Category.SelectedItem.value + "'", myConnection)

          DS = new DataSet()
          MyCommand.FillDataSet(DS, "Titles")

          MyList.DataSource = DS.Tables("Titles").DefaultView
          MyList.DataBind()

       End Sub

   </script>

   <body>

       <center>

       <form action="intro8.aspx" method="post" runat="server">

           <asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>

           <h3> Name: <asp:textbox id="Name" runat="server"/>

           Category:  <asp:dropdownlist id="Category" runat=server>
                         <asp:listitem >psychology</asp:listitem>
                         <asp:listitem >business</asp:listitem>
                         <asp:listitem >popular_comp</asp:listitem>
                      </asp:dropdownlist>

           <asp:button type=submit text="Lookup" onClick="SubmitBtn_Click" runat="server"/>

           <p>

           <asp:datalist id="MyList" repeatcolumns="2" borderwidth="0" runat="server">

             <template name="itemtemplate">

                 <table>
                   <tr>

                     <td>
                         <img src='<%# DataBinder.Eval
(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>'>
                     </td>

                     <td width=250 valign=top>

                         <b><%# DataBinder.Eval(Container.DataItem, "title") %></b>

                         <br><br>

                         Price: <%# DataBinder.Eval(Container.DataItem, "price", "${0}") %>
                     </td>

                   </tr>
                 </table>

             </template>

           </asp:datalist>

       </form>

       </center>

   </body>

</html>


这个程序的运行例子在
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro8.aspx

原创粉丝点击