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

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绑定的两种方法的更多相关文章

  1. Centos7网卡绑定的两种方法

    https://blog.51cto.com/youdong/1963416 选择模式4,并且layer选择3+4,交换机要配置LAG 一.传统的bond方式 (1)bond几种主要模式介绍 ü mo ...

  2. Java学习笔记——可视化Swing中JTable控件绑定SQL数据源的两种方法

    在 MyEclipse 的可视化 Swing 中,有 JTable 控件. JTable 用来显示和编辑常规二维单元表. 那么,如何将 数据库SQL中的数据绑定至JTable中呢? 在这里,提供两种方 ...

  3. WinForm中DataReader绑定到DataGridView的两种方法

    在WinForm中,DataReader是不能直接绑定到DataGridView的,我想到了用两种方法来实现将DataReader绑定到DataGridView. SqlCommand command ...

  4. .net中创建xml文件的两种方法

    .net中创建xml文件的两种方法 方法1:根据xml结构一步一步构建xml文档,保存文件(动态方式) 方法2:直接加载xml结构,保存文件(固定方式) 方法1:动态创建xml文档 根据传递的值,构建 ...

  5. jquery阻止元素冒泡的两种方法

    通常情况下,如果给父元素添加事件之后,子元素也会继承同样的事件,这个时候就要阻止子元素的这种行为,成为阻止冒泡,总结两种解决方法: html代码: <div id="parent&qu ...

  6. 安卓ListView操作的两种方法

    举例做一个微信的中间部分(好友消息等信息通知) 第一种:BaseAdapter() package com.example.wx; import java.util.ArrayList;import ...

  7. DataGridView显示数据的两种方法

    1.简介 DataGridView空间是我们经常使用的显示数据的控件,它有极高的可配置性和可扩展性. 2.显示数据 DataGridView显示数据一般我们经常使用的有两种方法,一种是直接设置Data ...

  8. 两种方法上传本地文件到github

    https://www.jianshu.com/p/c70ca3a02087 自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的 ...

  9. 两种方法上传本地文件到github(转)

    自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的原因是我习惯本地编辑,完成以后再一起上传github.看过了几个教程,总结出最 ...

随机推荐

  1. 从今天开始写博客、托管代码到 Github

    最近看了一篇文章,译名<简历危险>,原名<Resumes are dangerous>. 作者为Alex Maccaw,他有一篇文章曾经在网上流传甚广——<Traveli ...

  2. int (*p)[4] p 是二级指针 二维数组 二级指针 .xml

    pre{ line-height:1; color:#2f88e4; background-color:#e9ffff; font-size:16px;}.sysFunc{color:#3d7477; ...

  3. 黑马程序员——Foundation之NSString和NSMutableString

    ------Java培训.Android培训.iOS培训.Net培训.期待与您交流!------ 在OC中NSString是一个非常重要的字符串类;和C语言的字符串不用的是,C语言的字符串是用双引号括 ...

  4. Windows Live Writer安装与使用

    无耻的转贴一份WLW的安转与使用指南 =========================转贴分隔线===========================   [Windows Live Writer安 ...

  5. 远程连接mongodb出现 no route to host 和 Connection refused

    部署好mongodb服务器后,在客户端安装好php的mongodb扩展,用程序连接mongodb服务器出错:no route to host.搜索了差不多一天的时候都没有相关的解决方法.最后在mong ...

  6. 关于Bean

    什么是Bean? 一个Bean 就是一个类.我们不必为制造任何的Bean而编写一些特殊的代码或者使用特殊的编程语言.事实上,我们唯一需要做的是略微地修改我们对我们方法命名的办法.方法名通知应用程序构建 ...

  7. Android签名用keytool和jarsigner制作apk文件

    生成证书 keytool -genkey -alias aeo_android.keystore -keyalg RSA -validity -keystore aeo_android.keystor ...

  8. virtualenv 和 virtualenvwrapper 实践

    virtualenv 首先来聊一下 virtualenv 是个什么鬼. 在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题:亦或者是在开发过程中不想让物理环境里充 ...

  9. linux下find查找命令用法

    Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...

  10. [转]前景检测算法--ViBe算法

    原文:http://blog.csdn.net/zouxy09/article/details/9622285 转自:http://blog.csdn.net/app_12062011/article ...