欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-03 22:51:37
本文给大家分享web  中 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控件绑定数据源的三种方法,希望对大家有所帮助