All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
DropDownList绑定数据库字段获取下拉列表值_[Asp.Net教程]

Writer: 归海一刀 Article type: Programming skills(编程技巧) Time: 2014/1/30 1:27:40 Browse times: 296 Comment times: 0

DropDownList绑定数据库字段获取下拉列表值_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

DropDownList绑定数据库字段获取下拉列表值_[Asp.Net教程]

















DropDownList 绑定数据库中的字段获取下拉列表值

基础数据绑定:用ListItem直接枚举出来,适用于不需要修改的类型列表。


设计家园
网页设计
网络编程
酷站欣赏


动态绑定方法一:动态绑定数据库中的字段。

SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();
string strSQL = "select * from CompanyType";
SqlDataAdapter ada = new SqlDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
ada.Fill(ds, "CompanyType");
DropDownList1.DataSource = ds.Tables["CompanyType"].DefaultView;
DropDownList1.DataValueField = ds.Tables["CompanyType"].Columns[1].ColumnName;
DropDownList1.DataTextField = ds.Tables["CompanyType"].Columns[1].ColumnName;
DropDownList1.DataBind();
ds.Dispose();

动态绑定方法二:利用DropDownList.Items.Add方法。

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();
try
{
conn.Open();
this.DropDownList1.Items.Add("");
string strSQL = "select CompanyType from CompanyType";
SqlCommand com = new SqlCommand(strSQL, conn);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
this.DropDownList1.Items.Add(dr["CompanyType"].ToString());
}
}
catch (Exception ex)
{
Response.Write("alert(’" + ex.Message.ToString() + "’)");
}
finally
{
conn.Close();
}
}
}


说明:UtilitySqlClass.OperateDataBase.ReturnConn();返回的是一个打开的conn对象。数据库操作类在命名空间UtilitySqlClass中定义。



































There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.