ASP与ASP.NET的差异:创建ASP页面
1.打开Visual Studio.NET集成开发环境:点击Start,点击Programs,点击Experience VS .NET Content,点击Lab 3,点击ASP Source。一个名为Authors.asp的空ASP页面文件会在Visual Studio .NET IDE中打开,如图1所示。
|  图1 ASP页面
 | 
2.敲入以下代码
- <%@ Language=VBScript %>  
- <HTML>  
- <HEAD>  
- <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">  
- <STYLE>  
- BODY { font:arial }  
- H1 { color:navy }  
- </STYLE>  
- </HEAD>  
- <BODY>  
- <DIV align=center>  
- <H1>Authors</H1>  
-  
- <%  
- '  
- ' Connecting to a database  
- '  
- dim cn  
- set cn = server.CreateObject("ADODB.Connection")  
- cn.Open "Provider=sqloledb;" _  
- & "Data Source=(local);" _  
- & "Initial Catalog=pubs;" _  
- & "User ID=sa"  
- ' Retrieving Data via the Recordset Object.  
- dim rs  
- set rs = server.CreateObject("ADODB.Recordset")  
- rs.Open "select au_fname, au_lname, phone from authors order by au_lname",cn   
- %>  
注意:
下面的代码是由静态HTML和服务器端脚本构成的,用一个循环把数据集(recordset)中的所有数据遍历出来。
- <TABLE border='1'>  
- <TR>  
- <TH>First Name</TH>  
- <TH>Last Name</TH>  
- <TH>Phone</TH>  
- </TR>  
- <%  
- do until rs.EOF  
- Response.Write "<TR>"  
- Response.Write "<TD>" & rs("au_fname") & "</TD>"  
- Response.Write "<TD>" & rs("au_lname") & "</TD>"  
- Response.Write "<TD>" & rs("phone") & "</TD>"  
- Response.Write "</TR>"  
- rs.MoveNext  
- loop  
- %>  
- </TABLE>  
-  
- <!-- Footer -->  
- <h5>Current as of <%Response.Write now%></h5>  
- </DIV>  
- </BODY>  
- </HTML>  
3.点击File,再点击Save Authors.asp。
4.关闭IDE。
ASP与ASP.NET的差异:观看ASP页面
1.观看ASP页面:点击Start,点击Programs,点击Experience VS.NET Content,点击Lab 3,再点击ASP。页面显示如图2。
|  图2 ASP页面显示结果
 | 
ASP与ASP.NET的差异:创建ASP.NET页面
1.打开Visual Studio.NET IDE:点击Start,点击Programs,点击Experience VS.NET Content,点击Lab 3,然后点击ASP .NET VB Source。一个名为Authors VB.aspx的空ASP.NET页面文件会在Visual Studio.NET IDE打开,如图3所示。
|  图3 ASP.NET页面
 | 
2.点击Visual Studio.NET窗口左下角的HTML按钮查看页面代码
3.敲入以下代码
注意:System.Data和System.Data.SqlClient名字空间(namespaces)被声明在页面顶端,所以这两个名字空间中的所有类可以在下面ASP.NET页面中可用。
- <%@ Import Namespace="System.Data" %>  
- <%@ Import Namespace="System.Data.SqlClient" %>  
-  
- <HTML>  
- <HEAD>  
- <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">  
- <STYLE>  
- BODY { font:arial }  
- H1 { color:navy }  
- </STYLE>  
-  
- </HEAD>  
- <BODY>  
- <DIV align=center>  
- <H1>Authors</H1> 
注意:服务器端脚本与静态HTML完全分离。你可以使用任何run-time语言,例如Microsoft Visual Basic?,Microsoft? Jscript?和C#。
- < script language="VB" runat="server">  
-  
- Sub Page_Load(Src As Object, E As EventArgs)   
-  
- Dim DS As DataSet  
- Dim MyConnection As SQLConnection  
- Dim MyCommand As SQLDataAdapter  
-  
- MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")  
- MyCommand=New SQLDataAdapter("select au_fname as 'First Name', au_lname as 'Last Name',Phone from Authors",MyConnection)  
注意:下面代码中的DataSet对象取代了Recordset对象,并请注意SQLDataAdapter对象中的fill方法。
- DS = new DataSet()  
- MyCommand.Fill(ds,"Authors ") 
注意:下面代码设置了DataGrid 控件的DataSource属性。注意DataSet对象中 Table集合,跟Recordset对象不同,DataSet对象可以包含多个表.
- grdAuthors.DataSource=ds.Tables("Authors").DefaultView  
注意:在下面代码中,DataGrid控件用DataBind方法载入数据,然后DataGrid控件以HTML表形式显示数据。
- grdAuthors.DataBind()  
- End Sub  
- </script> 
注意:下面第一行代码往页面中嵌入了一个DataGrid对象。DataGrid控件的其它属性也可以通过加入属性/值对来设置,例如:Width="700" BackColor="#ccccff"。
- <asp:DataGrid runat=server id=grdAuthors/>   
-  
- <!-- Footer -->  
- <h5>Current as of <%Response.Write (Now.ToString)%></h5>  
- </DIV>  
-  
- </BODY>  
- </HTML>  
4.点击File,再点击Save Authors VB.aspx。
5.关闭IDE。
ASP与ASP.NET的差异:观看ASP.NET页面
1.查看ASP.NET页面:点击Start,点击Programs,点击Experience VS .NET Content,点击Lab 3,再点击ASP.NET-VB。页面显示如图4。
|  图4 ASP.NET显示页面
 | 
ASP与ASP.NET的差异介绍结束
当你完成了查看ASP.NET页面的工作,关闭所有窗口。