ASP+ 給我們提供了一套數據表格和數據列表的控件。這些控件可以幫助我們定制我們UI(user interFace 用戶界面)而不去考
慮一種數據庫或者其他的數據庫。例如:在下面的例子中,我們將要介紹一下<ASP:datagrid runat=server>控件是怎麼樣通過
sql 語句給我們提供數據的
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<Html>
<head>
<link rel="stylesheet"href="intro.CSS">
</head>
<script language="VB" runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyConnection As SQLConnection
Dim MyCommand As SQLDataSetCommand
'以下是數據庫聯結
MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
MyCommand = New SQLDataSetCommand("select * from Titles where type='" +
Category.SelectedItem.Value + "'", myConnection)
DS = new DataSet()
MyCommand.FillDataSet(DS, "Titles")
MyList.DataSource = DS.Tables("Titles").DefaultVIEw
MyList.DataBind()
End Sub
</script>
<body>
<center>
<form action="intro75.ASPx" method="post" runat="server">
<ASP:adrotator AdvertisementFile="ads.XML" BorderColor="black" BorderWidth=1 runat="server"/>
<h3> Name: <ASP:textbox id="Name" runat="server"/>
Category: <ASP:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</ASP:listitem>
<asp:listitem >business</ASP:listitem>
<asp:listitem >popular_comp</ASP:listitem>
</ASP:dropdownlist>
<ASP:button type=submit text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>
<p>
<ASP:DataGrid id="MyList" HeaderStyle-BackColor="#aaaadd" BackColor="#ccccff" runat="server"/>
</form>
</center>
</body>
</Html>
這個例子的運行示例在
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro75.ASPx
數據表格(data grid)控件 <ASP:datagrid runat=server>
給我們提供了一種非常簡單的方法用傳統的UI截面去顯示數據查詢的結果.Asp+ 的開發者現在還可以通過<ASP:dataList
runat=server>
來定制數據列表顯示來定制信息
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<Html>
<head>
<link rel="stylesheet"href="intro.CSS">
</head>
<script language="VB" runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyConnection As SQLConnection
Dim MyCommand As SQLDataSetCommand
MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
MyCommand = New SQLDataSetCommand("select * from Titles where type='" +
Category.SelectedItem.Value + "'", myConnection)
DS = new DataSet()
MyCommand.FillDataSet(DS, "Titles")
MyList.DataSource = DS.Tables("Titles").DefaultVIEw
MyList.DataBind()
End Sub
</script>
<body>
<center>
<form action="intro8.ASPx" method="post" runat="server">
<ASP:adrotator AdvertisementFile="ads.XML" BorderColor="black" BorderWidth=1 runat="server"/>
<h3> Name: <ASP:textbox id="Name" runat="server"/>
Category: <ASP:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</ASP:listitem>
<asp:listitem >business</ASP:listitem>
<asp:listitem >popular_comp</ASP:listitem>
</ASP:dropdownlist>
<ASP:button type=submit text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>
<p>
<ASP:datalist id="MyList" repeatcolumns="2" borderwidth="0" runat="server">
<template name="itemtemplate">
<table>
<tr>
<td>
<img src='<%# DataBinder.Eval
(Container.DataItem, "title_id", "/quickstart/ASPplus/images/title-{0}.gif") %>'>
</td>
<td width=250 valign=top>
<b><%# DataBinder.Eval(Container.DataItem, "title") %></b>
<br><br>
PRice: <%# DataBinder.Eval(Container.DataItem, "price", "${0}") %>
</td>
</tr>
</table>
</template>
</ASP:datalist>
</form>
</center>
</body>
</Html>