本文介绍了在下拉列表中绑定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在数据库的下拉列表中绑定数据
How to bind data in drop-down from the database
推荐答案
string SqlConnect = System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
SqlConnection Sqlconn = new SqlConnection(SqlConnect);
SqlCommand sqlcomm = new SqlCommand("select * from Category", Sqlconn);
Sqlconn.Open();
SqlDataReader dr = sqlcomm.ExecuteReader();
if (dr.HasRows)
{
DropDownList1.DataSource = dr;
DropDownList1.DataTextField = "CatName";
DropDownList1.DataValueField = "Catid";
DropDownList1.DataBind();
ListItem c = new ListItem("Select Category", "0");
DropDownList1.Items.Insert(0, c);
dr.Close();
Sqlconn.Close();
}
这篇关于在下拉列表中绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!