Asp.net| Object | A DataSet object is a collection of data tables (DataTables), where multiple DataTable can be stored in a dataset. As a data source, a dataset can provide multiple data tables, which can be associated with tables in a dataset, and each DataTable object can also be used as a data source. The following example demonstrates how a DataTable object is provided to a DropDownList control to provide a data source.
Create a new Web form, named Datatable_simple.aspx, and add two DropDownList controls, as shown in the design window as 8.9.
Figure 8.9 datatable_simple.aspx Design window
To toggle the Datatable_simple.aspx HTML Code window, the HTML code is as follows:
<%@ 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> save data using 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= "Song Body" >
<table id= "Table1" cellspacing= "1" cellpadding= "1" width= "360" "border=" 1 "style=" width:360px; height:75px ">
<TR>
<TD align= "Center" ><STRONG> use DataTable to save data </STRONG></TD>
</TR>
<TR>
<TD> section
<asp:dropdownlist id= "DropDownList1" runat= "Server" width= "107px" autopostback= "True" ></asp:dropdownlist The item value of the > item is-->
<asp:dropdownlist id= "DropDownList2" runat= "Server" ></asp:DropDownList></TD>
</TR>
</TABLE>
</FONT>
</form>
</body>
</HTML>
View the datatable_simple.aspx logical code (that is, the DataTable_Simple.aspx.vb file), where the logical code is as follows:
'--------cdoe begin----------
Public Class Datatable_simple
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' This call is required by the Web Forms Designer.
<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
' NOTE: The following placeholder declarations are required by the Web Forms Designer.
' Do not delete or move it.
Private Designerplaceholderdeclaration as System.Object
Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' CodeGen: This method call is required by the Web Forms Designer
' Do not modify it using the Code Editor.
InitializeComponent ()
End Sub
#End Region
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
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
' Generate DataTable object functions and generate data to store data in a DataTable object
Function DT () as DataTable
' Defines a DataTable object ' mydatatable ', and instantiates
Dim mydatatable as DataTable = New DataTable ("Mydatatable")
' Declare DataColumn object ' Mydatacolumn ' and DataRow object ' MyDataRow '
Dim Mydatacolumn as DataColumn
Dim MyDataRow as DataRow
' Materialized Mydatacolumn
Mydatacolumn = New DataColumn
Mydatacolumn.datatype = System.Type.GetType ("System.Int32")
Mydatacolumn.columnname = "id"
MYDATATABLE.COLUMNS.ADD (Mydatacolumn)
' Instantiate mydatacolumn
Mydatacolumn = New DataColumn
mydatacolumn.datatype = Type.GetType (" System.String ")
mydatacolumn.columnname =" Item "
myDataTable.Columns.Add (mydatacolumn)
' Instantiate MyDataRow and add to 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
' Return to Mydatatable
Return mydatatable
End Function
Private Sub dropdownlist1_selectedindexchanged (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Dropdo Wnlist1.selectedindexchanged
Dropdownlist2.selectedindex = Dropdownlist1.selectedindex
End Sub
End Class
'---------code end-----------
After you save the compilation, the Datatable_simple.aspx run as shown in Figure 8.10.
Figure 8.10 Datatable_simple.aspx Running effect