ASP.NET HTML 控件 数据库链接 - 绑定一个 DataList 控件

asp.net html 控件 数据库链接 - 绑定一个 datalist 控件

<%@ import namespace="system.data.oledb" %>

<script  runat="server">
sub page_load
dim dbconn,sql,dbcomm,dbread
dbconn=new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("/db/northwind.mdb"))
dbconn.open()
sql="select * from customers"
dbcomm=new oledbcommand(sql,dbconn)
dbread=dbcomm.executereader()
customers.datasource=dbread
customers.databind()
dbread.close()
dbconn.close()
end sub
</script>


<!doctype html>
<html>
<body>

<form runat="server">
<asp:datalist
id="customers"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="true"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
footerstyle-font-size="9pt"
footerstyle-font-italic="true">

<headertemplate>
customers table
</headertemplate>

<itemtemplate>
<%#container.dataitem("companyname")%>  in
<%#container.dataitem("address")%><%#container.dataitem("city")%>
</itemtemplate>

<footertemplate>
source: northwind database
</footertemplate>

</asp:datalist>
</form>

</body>
</html>
相关文章