ASP+中取代ASP的RS(Remote Scripting)技术的Framework

ASP+中取代ASP的RS(Remote Scripting)技术的Framework
Batman
在上面的文章里面我提到了Page.IsPostBack属性的一个应用,可以用来保存用户输入的
信息,下面我将介绍它的另外一个用处,那就是取代ASP中的RS(Remote Scripting)技术。
至于RS的基本概念和用法我已经在asp版里面有很多介绍了,它主要的优势就是在不刷新
当前页面的情况下和服务器端进行通信。但是由于它的底层是使用了java技术,所以它用
起来还是显得较为烦琐,下面我就将介绍在ASP+中如何利用Page.IsPostBack来取代RS技术。
按照我的习惯是喜欢用具体的例子来解释问题,所以这次还是使用一个简单的实例来说明
问题。下面这个例子中,将使用一个Products.aspx程序,它主要有两个服务器端控件(Server-side
control),这是asp+里面引入的新的控件编程方式,一个是一个下拉框控件--'mudCategories',
另外一个是列表框控件--'mudProducts'。这个例子将演示,列表框中的内容将跟随下拉框中内容
的改变而改变,为了大家重现的方便,我将使用SQL Server中自带的数据库例子来实现。

Products.aspx代码如下:

1@ Import Namespace="System.Data"
1@ Import Namespace="System.Data.ADO"
 1<script language="VB" runat="server">   
 2Sub Page_Load (SourceObj as Object, EveArg as EventArgs)   
 3  
 4If Not Page.IsPostBack Then   
 5Dim mudCommand As ADODataSetCommand   
 6Dim mudConnection As ADOConnection   
 7Dim dSet As New DataSet   
 8Dim strSQL as String   
 9Dim connStr as String   
10  
11strSQL = "SELECT CategoryID, CategoryName From Categories"   
12connStr = "Provider = SQLOLEDB; Data Source=test; Initial Catalog=Northwind; User ID=sa; password=;"   
13  
14mudConnection = New ADOConnection(connStr)   
15mudCommand = New ADODataSetCommand(strSQL, mudConnection)   
16mudCommand.FillDataSet(dSet, "Categories")   
17mudCategories.DataSource = dSet.Tables("Categories").DefaultView   
18mudCategories.DataBind()   
19End If   
20End Sub   
21  
22Sub displayProducts (Source as Object, EveArg as EventArgs)   
23  
24Dim mudCommand As ADODataSetCommand   
25Dim mudConnection As ADOConnection   
26Dim dSet As New DataSet   
27Dim strSQL as String   
28Dim connStr as String   
29  
30connStr = "Provider = SQLOLEDB; Data Source=test; Initial Catalog=Northwind; User ID=sa; password=;"   
31  
32strSQL = "Select ProductID, ProductName From Products"   
33strSQL = strSQL & " WHERE CategoryID = " & mudCategories.SelectedItem.Value   
34  
35mudConnection = New ADOConnection(connStr)   
36mudCommand = New ADODataSetCommand(strSQL, mudConnection)   
37mudCommand.FillDataSet(dSet, "Products")   
38mudProducts.DataSource = dSet.Tables("Products").DefaultView   
39mudProducts.DataBind()   
40End Sub   
41  
42</script>
1<html>
2<form name="mudForm" runat="server">   
3  
4产品目录:   
5<asp:dropdownlist autopostback="true" datatextfield="CategoryName" datavaluefield="CategoryID" id="mudCategories" onselectedindexchanged="displayProducts" runat="server"></asp:dropdownlist>   
6产品: <asp:listbox datatextfield="ProductName" datavaluefield="ProductID" id="mudProducts" runat="server" selectionmode="Multiple"></asp:listbox>
7</form></html>

从例子程序可以看到,其中显然是使用了服务器端控件的下拉框OnChange事件配合AutoPostBack和
Page.IsPostBack属性就可以很简单和清晰的实现了以前在asp中烦琐的RS实现方法。
呵呵,希望大家从上面的这个例子中有所收获。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus