DataView 对象也可以当成数据源使用。下列范例利用RadioButtonList Web 控件将Members数据表的DefaultView 对象当成数据源使用:
<%@Import Namespace=System.Data.ADO%>
<%@Import Namespace=System.Data%>
<Html>
<Form Id=Form1 Runat="Server">
请选择一个使用者:
<ASP:RadioButtonList Id="rblA" AutoPostBack="True"
OnSelectedIndexChanged="rblA_Chg" Runat="Server"/>
</Form>
<ASP:Label Id="Label1" Runat="Server"/>
<Script Language="VB" Runat="Server">
Sub Page_Load(Sender As Object, e As EventArgs)
If Page.IsPostBack=False Then '连接数据库读取数据及系结的工作只要作一
次
Dim strConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\InetPub\wwwroot\CR\CH05\MyWeb.mdb"
Dim strComStr As String = "Select * From Members"
Dim dscA As ADODataSetCommand = New
ADODataSetCommand(strComStr,strConStr)
Dim dsDataSet As DataSet = New DataSet()
dscA.FillDataSet(dsDataSet, "Members")
rblA.DataSource=dsDataSet.Tables("Members").DefaultView '将
DataTable 当成数据源
rblA.DataTextField="UserName" '指定要显示的字段
rblA.DataValueField="UserId" '指定这个字段代表的值
Page.DataBind()
End If
End Sub
Sub rblA_Chg(Sender As Object, e As EventArgs)
Label1.Text="选择项目为: " & rblA.SelectedItem.Text & _
"<br>所代表值为: " & rblA.SelectedItem.Value
End Sub
</SCRIPT>
</Html>