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="http://schemas.microsoft.com/intellisense/ie5" 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" style="WIDTH: 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 Form設計器產生的程式碼 "
'該調用是 Web Form設計器所必需的。
<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 Form設計器所必需的。
'不要刪除或移動它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法調用是 Web Form設計器所必需的
'不要使用代碼編輯器修改它。
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運行效果