DropDownList控件绑定数据源的三种方法

本文给大家分享web  中 DropDownList绑定数据源的几种方式,先给大家分享三种常见的方式,具体详情如下所示:

 第一种  

this.ddltype.DataTextField = "btName";//显示的值

this.ddltype.DataValueField = "btId";//获取dropdownlist中的值

ddltype.DataSource = service.GetBusinessTypeAll("");

this.ddltype.DataBind();

第二种           

DataTable dt_GetBusinessName = service.GetBusinessTypeAll("");

//表中无数据的话直接返回

if (dt_GetBusinessName == null)

return;

for (int i = 0; i < dt_GetBusinessName.Rows.Count; i++)

{

ddltype.Items.Add(new ListItem(dt_GetBusinessName.Rows[i]["btName"].ToString().Trim(),//显示的值

dt_GetBusinessName.Rows[i]["btId"].ToString().Trim()));

}s

ddltype.Items.Add(new ListItem("全部", "-1"));

ddltype.Text = "全部";

如何选中DropDownList1中的值用

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

this.TextBox1.Text = this.DropDownList1.SelectedItem.Text.ToString();

}

第三种:

protected void ddl_bumen_SelectedIndexChanged(object sender, EventArgs e)

{

if (this.ddl_bumen.Text != "选择部门")

{

int id = Int32.Parse(this.ddl_bumen.Text);

SqlParameter bumen = new SqlParameter("@dept_id", id);

IList<lbEmploy> list = BLLGeneric.Select<lbEmploy>(CommandType.StoredProcedure, "lb_liandong", bumen);

//配置数据源

this.ddl_em.DataSource = list;

//绑定字段属性

this.ddl_em.DataTextField = "Em_name";

this.ddl_em.DataValueField = "Em_name";

//绑定控件

this.ddl_em.DataBind();

}

else

{

this.Response.Write(string.Format(GetRedirect.WINALERT, "请选择部门"));

this.ddl_bumen.Focus();

return;

}

}

下面看下DropdownList控件动态绑定数据源的两种方法

DropdownList控件绑定数据源示例,本文提供两种方法,皆为动态绑定

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

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("<scirpt>alert('" + ex.Message.ToString() + "')</script>");

}

finally

{

conn.Close();

}

}

}

第一种方法:

string ConnString = ConfigurationSettings.AppSettings["ConnectionString"];

//创建一个SqlConnection

SqlConnection Conn = new SqlConnection( ConnString );

string SQL_Select = "select id, ItemName from DDLItem order by id desc";

//构造一个SqlDataAdapter

SqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select, Conn);

//开始读取数据

Conn.Open();

DataSet dataSet = new DataSet();

myAdapter.Fill( dataSet,"Table1" );

Conn.Close();

//开始绑定DropDownList

//指定DropDownList使用的数据源

DropDownList1.DataSource = dataSet.Tables["Table1"].DefaultView;

//指定DropDownList使用的表里的那些字段

DropDownList1.DataTextField = "ItemName"; //dropdownlist的Text的字段

DropDownList1.DataValueField = "id";//dropdownlist的Value的字段

DropDownList1.DataBind();

第二种方法:

con.Open();

SqlCommand cmd = new SqlCommand(strSql,con);

SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())

{

DropDownList1.Items.Add(new ListItem(dr["status"].ToString(), dr["status_Id"].ToString()));

}

以上所述是小编给大家介绍的DropDownList控件绑定数据源的三种方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

以上是 DropDownList控件绑定数据源的三种方法 的全部内容, 来源链接: utcz.com/z/340158.html

回到顶部