從資料庫檢索資料填充DropDownList 下拉式功能表

來源:互聯網
上載者:User

<%@ Page Language="VB" Debug="true"%>
<%@ import namespace="system.data.oledb"%>
<%@ import namespace="system.data"%>
<html><head>
<script runat="server">
Sub Page_init(Src As Object, E As EventArgs)
dim dbconn as oledbconnection '聲明一個資料庫連接對象
dim dbcommand as oledbdataadapter '利用oledbdataadapter對象聲明一個資料檢索
dim dspagedata as new dataset  '利用dataset儲存檢索結果的資料
dbconn=new oledbconnection("provider=sqloledb;server=thc;initial catalog=thc;user id=sa;pwd=980123;") '資料庫連接
dbcommand = new oledbdataadapter("select id,quert,name from thcjp", dbconn) '從thcjp表中檢索資料
dbcommand.fill(dspagedata,"thcjp") '把紀錄放在dataset對象中一個名為“thcjp”的表中
dbcommand.fill(dspagedata,"thcjp1")
ddlthcjp.datasource=dspagedata.tables("thcjp").defaultview '定義所用dataset表的名字
ddlthcjp1.datasource=dspagedata.tables("thcjp1").defaultview
ddlthcjp.databind() '綁定這個表
ddlthcjp1.databind()
End Sub
sub btn_click(Src As Object, E As EventArgs)
lbl.text="先頭那個框框寫的是:" & ddlthcjp.selecteditem.text & "<br>第二個框框裡頭選的是: "& ddlthcjp1.selecteditem.text
end sub
</script>
</head><body>
<form runat="server">
<asp:DropDownList ID="ddlthcjp" runat="server" DataTextField="name" DataValueField="id"/>
<asp:DropDownList ID="ddlthcjp1" runat="server" DataTextField="quert" DataValueField="id"/>
<asp:Button ID="btn" runat="server" Text="提交" OnClick="btn_click" />
</form>
<asp:Label ID="lbl" runat="server"/>
</body></html>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.