System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
sqlconn.C;
sqlconn.Open();
System.Data.SqlClient.SqlDataAdapter sqldar = new System.Data.SqlClient.SqlDataAdapter("select UserName from forums_Users",sqlconn);
sqldar.SelectCommand.CommandType = CommandType.Text;
System.Data.DataSet DataSet1= new System.Data.DataSet();
sqldar.Fill(DataSet1,"Users");
DropDownList1.DataSource = DataSet1.Tables["Users"].DefaultView;
DropDownList1.DataTextField="UsersName";
DropDownList1.DataBind();
sqlconn.Close();
第一行是创建一个sql连接对象sqlconn;
第二行是给新建的sql连接对象sqlconn的连接字符串赋予正确的值;
第三行是让sql连接对象sqlconn打开,连接sql数据库;
第四行是创建一个sql适配器对象sqldar,并同时让其使用sqlconn对象执行一条sql查询语句;
第五行是设置sqldar对象的命令类型为文本型;
第六行是创建一个数据集对象DataSet1;
第七行是将sqldar执行的结果填充到DataSet1中,并将命名为Users;
第八行是将DropDownList的数据源设置为DataSet1的Users,并使用默认的查看模式;
第九行是设置DropDownList空间的显示项对应的字段名UsersName;
第十行是执行DropDownList的数据绑定方法;
第十一行是关闭sqlconn对象。

*** 这段代码就是实现了DropDownList的选择项为Users表中的UsersName,只是一个简单的示范,通常在使用过长中会更细致甚至更麻烦一些,但是基本的思想是一样的:创建你需要的数据源,让指定的server端web控件与你的数据源绑定,指定显示项和显示项对应的value。

绑定dropdownlist的更多相关文章

  1. repeater绑定dropdownlist,jquery+ajax页面无刷新,修改dropdownlist默认值

    html代码: <td>                        <asp:HiddenField ID="hiddenchuli" Value='< ...

  2. Asp.Net MVC绑定DropDownList等控件

    测试环境:vs2013..Net4.5.mvc5 一.Asp.Net MVC绑定控件原理说明 以Html.TextBox为例 /// <param name="name"&g ...

  3. Repeater的Item项绑定DropDownList

    前台页面: <asp:Repeater ID="rptJgtList" runat="server" OnItemDataBound="rptJ ...

  4. GridView 中绑定DropDownList ,下拉框默认选中Label的值

    在GridView中,我们 有时候要绑定值. 前台绑定的代码可以这样 <asp:TemplateField HeaderText="当前状态" ItemStyle-Horiz ...

  5. ASP.NET Repeater 绑定 DropDownList Calendar 选择日期

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. Gridview中绑定DropDownList

    1.页面代码                         <asp:TemplateField HeaderText="等级">                   ...

  7. MVC后台绑定dropdownList

     public ActionResult Index()         {             List<SelectListItem> items = new List<Se ...

  8. MVC 枚举绑定 DropDownList

    /// <summary> /// 枚举转化下拉列表数据集 /// </summary> /// <param name="type">类型&l ...

  9. 在GridView控件里面绑定DropDownList控件

    参考链接: http://www.aspsnippets.com/Articles/Populate-DropDownList-with-Selected-Value-in-EditItemTempl ...

随机推荐

  1. 南京Uber优步司机奖励政策(1月11日~1月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. AGG 之贝塞尔插值

    原文: http://liyiwen.iteye.com/blog/705489 之前 comp.graphic.algorithms 上有一个讨论,是关于怎么样使用曲线对多边形进行插值处理,使得最终 ...

  3. [IE9] GPU硬件加速

      IE9 的一个重大改进就是使用了GPU硬件加速来渲染网页. 那么GPU硬件加速到底能够带来多大的性能提升? 你可以在IE的测试案例网站(http://ie.microsoft.com/testdr ...

  4. PHP面试题(二)

    前言 从网上找了一套号称是百度的php面试题目,这里记录一下 PHP的gc机制 php的垃圾回收机制注意以下几点即可: 引用计数refcount和is_ref,也就是php不会随意的malloc内存空 ...

  5. LINUX curl GET 掉参数解决办法

    LINUX curl GET 掉参数解决方法 url 为 http://mywebsite.com/index.php?a=1&b=2&c=3web形式下访问url地址,使用 $_GE ...

  6. Spring 基础知识

    Spring架构简单描述 原文:https://www.shiyanlou.com/courses/document/212 Spring 概述 1. Spring 是什么 Spring是一个开源的轻 ...

  7. 学习restful 架构

    越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...

  8. 自我理解foreach工作原理

        很多时候我们在使用for循环遍历一个数组的时候,我们都知道可以通过下标的索引找到当前数组中所对应的数据.这只对于简单的数组或集合,如果我们存储的数据不止只有数据项,还有一个标识项,就如同Has ...

  9. Monkeyrunner入门示例

    准备工作1.安装Android SDK2.熟悉MonkeyRunner的API(http://article.yeeyan.org/view/37503/164523)3.一部Android手机或模拟 ...

  10. [ES6] Array.find()

    Convenient method to find one item in an array, avoid writing and  for + if: let arys = [1,,5,,6] ; ...