DropDownList绑定数据库字段
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
// Insert page code here
private void Page_Load(object sender,System.EventArgs e)
{
DropDownListAdd();
}
void DropDownListAdd()
{
string strconn="Data Source=localhost;Initial Catalog=hgyp;Integrated Security=SSPI";
SqlConnection conn=new SqlConnection(strconn);
conn.Open();
string strsql="select * from zs_class";
SqlDataAdapter myDataAdapter=new SqlDataAdapter(strsql,conn);
DataSet ds=new DataSet();
myDataAdapter.Fill(ds,"zs_class");
DropDownList1.DataSource=ds.Tables["zs_class"].DefaultView;
DropDownList1.DataBind();
conn.Close();
}
//
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p align="center">
<asp:DropDownList id="DropDownList1" runat="server" DataValueField="classid" DataTextField="classname" Width="160px"></asp:DropDownList>
</p>
<p align="center">
<asp:TextBox id="TextBox1" runat="server" Width="160px"></asp:TextBox>
</p>
<p align="center">
<asp:Button id="Button1" runat="server" Text="确定"></asp:Button>
</p>
<!-- Insert content here -->
</form>
</body>
</html>