Asp+文法介紹(六)----資料庫篇
作者:Eve Cole
更新時間:2009-05-30 19:54:02
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>
這個程式的運行範例在
http://tutorial.superexpert.com/quickstart/aspplus/samples/webforms/intro/intro8.aspx