日期:2009-02-23 浏览次数:20538 次
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 If
End Sub
Sub 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实现方法。
呵呵,希望大家从上面的这个例子中有所收获。