ASP+中取代ASP的RS(Remote Scripting)技术的Framework
文章出处:联动天下 作者:联动天下 发布时间:2005-12-21
来自Active.com.cn
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代码如下:
代码:
---------------------------------------------------------------------
<%@ Import Namespace="System.Data"%><%@ Import Namespace="System.Data.ADO"%><script language="VB"
runat="server">Sub Page_Load (SourceObj as Object, EveArg as EventArgs)If Not Page.IsPostBack Then
Dim mudCommand As ADODataSetCommand Dim mudConnection As ADOConnection Dim dSet As New DataSet
Dim strSQL as String Dim connStr as String strSQL = "SELECT CategoryID, CategoryName
From Categories" connStr = "Provider = SQLOLEDB; Data Source=test; Initial Catalog=Northwind;
User ID=sa; password=;" mudConnection = New ADOConnection(connStr) mudCommand = New
ADODataSetCommand(strSQL, mudConnection) mudCommand.FillDataSet
(dSet, "Categories") mudCategories.DataSource = dSet.Tables("Categories").DefaultView
mudCategories.DataBind()End IfEnd SubSub displayProducts (Source as Object, EveArg as EventArgs) Dim
mudCommand As ADODataSetCommand Dim mudConnection As ADOConnection Dim dSet As New DataSet Dim
strSQL as String Dim connStr as String connStr = "Provider = SQLOLEDB; Data Source=test; Initial
Catalog=Northwind; User ID=sa; password=;" strSQL = "Select ProductID, ProductName From
Products" strSQL = strSQL & " WHERE CategoryID = " & mudCategories.SelectedItem.Value
mudConnection = New ADOConnection(connStr) mudCommand = New ADODataSetCommand(strSQL,
mudConnection) mudCommand.FillDataSet(dSet, "Products") mudProducts.DataSource =
dSet.Tables("Products").DefaultView mudProducts.DataBind()End Sub </script><html><form
name="mudForm" runat="server"> 产品目录: <asp:DropDownList id="mudCategories" runat="server"
DataTextField="CategoryName" DataValueField="CategoryID" AutoPostBack="true"
OnSelectedIndexChanged="displayProducts"/>产品: <asp:ListBox id="mudProducts" SelectionMode="Multiple"
DataValueField="ProductID" DataTextField="ProductName" runat="server" /></html>
----------------------------------------------------------------------
从例子程序可以看到,其中显然是使用了服务器端控件的下拉框OnChange事件配合AutoPostBack和Page.IsPostBack属性
就可以很简单和清晰的实现了以前在asp中烦琐的RS实现方法。
呵呵,希望大家从上面的这个例子中有所收获。
----------------------------------------------------------------------
文章出处:联动天下 作者:联动天下 发布时间:2005-12-21
来自Active.com.cn
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代码如下:
代码:
---------------------------------------------------------------------
<%@ Import Namespace="System.Data"%><%@ Import Namespace="System.Data.ADO"%><script language="VB"
runat="server">Sub Page_Load (SourceObj as Object, EveArg as EventArgs)If Not Page.IsPostBack Then
Dim mudCommand As ADODataSetCommand Dim mudConnection As ADOConnection Dim dSet As New DataSet
Dim strSQL as String Dim connStr as String strSQL = "SELECT CategoryID, CategoryName
From Categories" connStr = "Provider = SQLOLEDB; Data Source=test; Initial Catalog=Northwind;
User ID=sa; password=;" mudConnection = New ADOConnection(connStr) mudCommand = New
ADODataSetCommand(strSQL, mudConnection) mudCommand.FillDataSet
(dSet, "Categories") mudCategories.DataSource = dSet.Tables("Categories").DefaultView
mudCategories.DataBind()End IfEnd SubSub displayProducts (Source as Object, EveArg as EventArgs) Dim
mudCommand As ADODataSetCommand Dim mudConnection As ADOConnection Dim dSet As New DataSet Dim
strSQL as String Dim connStr as String connStr = "Provider = SQLOLEDB; Data Source=test; Initial
Catalog=Northwind; User ID=sa; password=;" strSQL = "Select ProductID, ProductName From
Products" strSQL = strSQL & " WHERE CategoryID = " & mudCategories.SelectedItem.Value
mudConnection = New ADOConnection(connStr) mudCommand = New ADODataSetCommand(strSQL,
mudConnection) mudCommand.FillDataSet(dSet, "Products") mudProducts.DataSource =
dSet.Tables("Products").DefaultView mudProducts.DataBind()End Sub </script><html><form
name="mudForm" runat="server"> 产品目录: <asp:DropDownList id="mudCategories" runat="server"
DataTextField="CategoryName" DataValueField="CategoryID" AutoPostBack="true"
OnSelectedIndexChanged="displayProducts"/>产品: <asp:ListBox id="mudProducts" SelectionMode="Multiple"
DataValueField="ProductID" DataTextField="ProductName" runat="server" /></html>
----------------------------------------------------------------------
从例子程序可以看到,其中显然是使用了服务器端控件的下拉框OnChange事件配合AutoPostBack和Page.IsPostBack属性
就可以很简单和清晰的实现了以前在asp中烦琐的RS实现方法。
呵呵,希望大家从上面的这个例子中有所收获。
----------------------------------------------------------------------