asp.net|对象|数据 DataSet对象是数据表的集合(DataTables),在一个DataSet中可以存放多个DataTable。作为数据源,DataSet可以提供多个数据表,在DataSet中可以进行表的关联操作工作,同样,每个DataTable对象也可以作为数据源。下面这个实例演示了DataTable对象如何提供给DropDownList控件提供数据源的。
新建一个Web窗体,命名为DataTable_Simple.aspx,添加两个DropDownList控件,设计窗口如8.9所示。
图 8.9 DataTable_Simple.aspx的设计窗口
切换DataTable_Simple.aspx的HTML代码窗口,HTML代码如下:
<% Page Language="vb" AutoEventWireup="false" Codebehind="DataTable_Simple.aspx.vb" Inherits="Adodotnet.DataTable_Simple"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>利用DataTable保存数据</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="360" border="1" 360px; HEIGHT: 75px">
<TR>
<TD align="center"><STRONG>利用DataTable保存数据</STRONG></TD>
</TR>
<TR>
<TD>第
<asp:DropDownList id="DropDownList1" runat="server" Width="107px" AutoPostBack="True"></asp:DropDownList>项的项值是-->
<asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList></TD>
</TR>
</TABLE>
</FONT>
</form>
</body>
</HTML>
查看DataTable_Simple.aspx的逻辑代码(即DataTable_Simple.aspx.vb文件),其中逻辑代码如下:
'--------cdoe begin----------
Public Class DataTable_Simple
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
Protected WithEvents DropDownList2 As System.Web.UI.WebControls.DropDownList
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not Page.IsPostBack Then
Dim mydt As DataTable
DropDownList1.DataSource = dt()
DropDownList1.DataTextField = "id"
DropDownList1.DataBind()
DropDownList2.DataSource = dt()
DropDownList2.DataTextField = "item"
DropDownList2.DataBind()
End If
End Sub
'生成DataTable对象函数,并生成数据,把数据保存在DataTable对象中
Function dt() As DataTable
' 定义一个DataTable对象“MyDataTable”,并实例化
Dim myDataTable As DataTable = New DataTable("MyDataTable")
' 声明 DataColumn对象“myDataColumn”和DataRow对象“ myDataRow”
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow
'实例化myDataColumn
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.Int32")
myDataColumn.ColumnName = "id"
myDataTable.Columns.Add(myDataColumn)
'实例化myDataColumn
myDataColumn = New DataColumn
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "item"
myDataTable.Columns.Add(myDataColumn)
' 实例化myDataRow并添加到myDataTable中
Dim i As Integer
For i = 0 To 10
myDataRow = myDataTable.NewRow
myDataRow("id") = i
myDataRow("item") = "item " & i
myDataTable.Rows.Add(myDataRow)
Next i
' 返回myDataTable
Return myDataTable
End Function
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
DropDownList2.SelectedIndex = DropDownList1.SelectedIndex
End Sub
End Class
'---------code end-----------
保存编译后,DataTable_Simple.aspx运行效果如图8.10所示。
图8.10 DataTable_Simple.aspx运行效果