国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

DropDownList控件綁定數(shù)據(jù)源的三種方法

2019-11-19 18:17:22
字體:
供稿:網(wǎng)友

本文給大家分享web  中 DropDownList綁定數(shù)據(jù)源的幾種方式,先給大家分享三種常見的方式,具體詳情如下所示:

 第一種  

   this.ddltype.DataTextField = "btName";//顯示的值        this.ddltype.DataValueField = "btId";//獲取dropdownlist中的值        ddltype.DataSource = service.GetBusinessTypeAll("");        this.ddltype.DataBind();

第二種           

 DataTable dt_GetBusinessName = service.GetBusinessTypeAll("");        //表中無數(shù)據(jù)的話直接返回        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);      //配置數(shù)據(jù)源      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, "請(qǐng)選擇部門"));      this.ddl_bumen.Focus();      return;    }  }

下面看下DropdownList控件動(dòng)態(tài)綁定數(shù)據(jù)源的兩種方法

DropdownList控件綁定數(shù)據(jù)源示例,本文提供兩種方法,皆為動(dòng)態(tài)綁定

動(dòng)態(tài)綁定方法一:動(dòng)態(tài)綁定數(shù)據(jù)庫中的字段。

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();

動(dòng)態(tài)綁定方法二:利用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"];     //創(chuàng)建一個(gè)SqlConnection     SqlConnection Conn = new SqlConnection( ConnString );         string SQL_Select = "select id, ItemName from DDLItem order by id desc";     //構(gòu)造一個(gè)SqlDataAdapter     SqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select, Conn);     //開始讀取數(shù)據(jù)     Conn.Open();     DataSet dataSet = new DataSet();     myAdapter.Fill( dataSet,"Table1" );     Conn.Close();     //開始綁定DropDownList     //指定DropDownList使用的數(shù)據(jù)源     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控件綁定數(shù)據(jù)源的三種方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 白玉县| 民乐县| 云梦县| 枝江市| 垣曲县| 佳木斯市| 建湖县| 大庆市| 西城区| 玉环县| 顺平县| 黔西县| 红桥区| 常山县| 新田县| 金平| 青龙| 馆陶县| 新竹市| 林州市| 虎林市| 天水市| 若羌县| 赤壁市| 永春县| 丰宁| 泽普县| 乐都县| 苍梧县| 南郑县| 武强县| 博兴县| 顺义区| 双城市| 怀柔区| 息烽县| 饶河县| 监利县| 庆城县| 盐山县| 绍兴县|