今天打算学习下dropdownlist控件的取值,当你通过数据库控件或dataset绑定值后,但又希望显示指定的值,这可不是简单的值绑定就OK,上网搜了一些资料,想彻底了解哈,后面发现其中有这么大的奥妙,可以通过很多种方法解决同样的问题,下面详说:

一、dropdownlist控件的值绑定方法:
1、直接输入item项
<asp:DropDownList ID="DropDownList1" runat="server" >
 <asp:ListItem>张三</asp:ListItem>
 <asp:ListItem>李四</asp:ListItem>
</asp:DropDownList>                     
这恐怕是最简单的,看下面这种
2、数据源控件绑定
<asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"DataTextField="name"DataValueField="name">                           </asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings: ConnectionString %>"
 SelectCommand="SELECT [name] FROM [yh]"></asp:SqlDataSource>
这种实用、方便写,再看下面这种
3、使用dataset或datareader绑定控件(以dataset为例)
SqlDataAdapter da = new SqlDataAdapter("select id,name from hy",conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField="name";
DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
这种高级一点,或许还有一些方法,发现中;
二、而实际应用中,很多时候不是简单的一个绑定值那么简单,例如:当dropdownlist控件绑定值后,而你又希望指定初始值,就是显示的值,例子很多就不举了,下面是自己总结的几种方法(只放前后台主要代码):
第一种:
前台代码:
<asp:DropDownList ID="DropDownList1" runat="server" >
 <asp:ListItem>张三</asp:ListItem>
 <asp:ListItem>李四</asp:ListItem>
</asp:DropDownList>
后台代码:
DropDownList1.Item.Inset(0,"李四");//这是插入第一个值为李四;
DropDownList.Items.FindByValue("李四").selected = true;//这是调用findbyvalue方法指定初始值;
第二种:
前台代码:
<asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"DataTextField="name"DataValueField="name">      
                      </asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings: ConnectionString %>"
 SelectCommand="SELECT [name] FROM [yh]"></asp:SqlDataSource>
后台代码:
DropDownList1.SelectedValue = "李四"; //使用item方法貌似不行,会提示没有引入实例错误;
第三种:
前台代码:前面2种都可以;
后台代码:
DropDownList1.SelectedIndex = 1;//通过控件索引来指定,1代表第二个值;
 
其实还有一种,比较经常用到,实例说明:(在此直观的说明)
实例问题:绑定控件的值为id,但显示为name,同样首先指定默认值,通过选项,修改id;
区别:默认值是通过数据库数值或传的数据来指定的,而不是指定一个默认字符串;
解决方法:
1、前台代码:
<asp:DropDownList ID="DropDownList1" runat="server" >
 <asp:ListItem>张三</asp:ListItem>
 <asp:ListItem>李四</asp:ListItem>
</asp:DropDownList>
后台代码:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
       {
          string yhid = Request.Params["userid"].ToString();                   
          DropDownList1.Items.FindByValue(yhid).Selected= true;
}
}//这里只是简单阐述,如果是从dataset读出来的值,是一样的效果;
2、前台代码:
<asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"DataTextField="name"DataValueField="id">                           </asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings: ConnectionString %>"
 SelectCommand="SELECT [id] [name] FROM [yh]"></asp:SqlDataSource>
后台代码:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          string yhid = Request.Params["userid"].ToString();                   
DropDownList1.SelectedValue = yhid; }
}
3、或者可以通过sql语句直接读取id所对应的name,就可以直接使用赋值了:
 
三、DropDownList数据绑定第一项为空的方法
DropDownList1.DataSource = ds.Tab
les[0];
DropDownList1.DataTextField="name";
DropDownList1.DataValueField = "id";
DropDownList1.Items.Insert(0,new ListItem());
下面为备注说明:
selectedindex获得的是选定项的索引,索引值是从0开始.
selectedvalue是所有选择的值.
selecteditem.value是获取索引值最小的选定项.如果是多选的情况下,selectedvalue和selecteditem.value就有这么点差别.
selecteditem代表选定项,相当于一个对象,这个对象仍然会有其他的属性,比如checked,Attributes,value,而selectedvalue就是一个值,是一个字符串

DropDownList 控件的更多相关文章

  1. DropDownList控件

    1.DropDownList控件 <asp:DropDownList runat="server" ID="DropDownList1" AutoPost ...

  2. DropDownList 控件不能触发SelectedIndexChanged 事件

    相信DropDownList 控件不能触发SelectedIndexChanged 事件已经不是什么新鲜事情了,原因也无外乎以下几种: 1.DropDownList 控件的属性 AutoPostBac ...

  3. 三级联动---DropDownList控件

    AutoPostBack属性:意思是自动回传,也就是说此控件值更改后是否和服务器进行交互比如Dropdownlist控件,若设置为True,则你更换下拉列表值时会刷新页面(如果是网页的话),设置为fl ...

  4. c#中DropDownList控件绑定枚举数据

    c# asp.net 中DropDownList控件绑定枚举数据 1.枚举(enum)代码: private enum heros { 德玛 = , 皇子 = , 大头 = , 剑圣 = , } 如果 ...

  5. DropDownList控件学习

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

  6. 客户端用JavaScript填充DropDownList控件 服务器端读不到值

    填充没有任何问题,但是在服务器端却取不出来下拉表中的内容.页面代码如下. <form id="form1" runat="server"> < ...

  7. DropDownList 控件的SelectedIndexChanged事件触发不了

    先看看网友的问题: 根据Asp.NET的机制,在html markup有写DropDownList控件与动态加载的控件有点不一样.如果把DropDownList控件写在html markup,即.as ...

  8. 在FooterTemplate内显示DropDownList控件

    如果想在Gridview控件FooterTemplate内显示DropDownList控件供用户添加数据时所应用.有两种方法可以实现,一种是在GridView控件的OnRowDataBound事件中写 ...

  9. 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定

    aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...

随机推荐

  1. hdu 5139 Formula

    http://acm.hdu.edu.cn/showproblem.php?pid=5139 思路:这道题要先找规律,f(n)=n!*(n-1)!*(n-2)!.....1!;  不能直接打表,而是离 ...

  2. Unity NGUI根据高度自适应屏幕分辨率

    Unity版本:4.5.1 NGUI版本:3.6.5 本文内容纯粹转载,转载保留参考链接和作者 参考链接:http://blog.csdn.net/asd237241291/article/detai ...

  3. Android WebView播放视频flash(判断是否安装flash插件)

    Android WebView播放flash(判断是否安装flash插件)  最近帮一个同学做一个项目,断断续续的一些知识点记录一下.一个页面中有一个WebView,用来播放swf,如果系统中未安装f ...

  4. (转载)调用ob_end_flush()网页仍旧不能显示有关问题

    (转载)http://www.myexception.cn/php/558638.html 调用ob_end_flush()网页仍旧不能显示问题?写了一个简单的demo,理论上调用ob_end_flu ...

  5. 网络流(最大费用最大流) :POJ 3680 Intervals

    Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7218   Accepted: 3011 Descrip ...

  6. 【模拟】Codeforces 704A & 705C Thor

    题目链接: http://codeforces.com/problemset/problem/704/A http://codeforces.com/problemset/problem/705/C ...

  7. Sqrt(x)——LeetCode

    Implement int sqrt(int x). Compute and return the square root of x. 题目大意:实现求一个int的根. 解题思路:二分. public ...

  8. c++学习(1)

    c++学习(1) 1.const C VS C++: 在c语言中const是一个只读变量(ReadOnly Varible),在c++中const只是代表常量(Constant). 例: const ...

  9. AOJ 0525 穷举

    题意:有一个烤饼器可以烤r行c列的煎饼,煎饼可以正面朝上(用1表示)也可以背面朝上(用0表示).一次可将同一行或同一列的煎饼全部翻转.现在需要把尽可能多的煎饼翻成正面朝上,问最多能使多少煎饼正面朝上? ...

  10. ServletConfig

    ServletConfig Servlet配置 比如web程序中的某一个Servlet需要配置一些初始化信息,需要在web.xml中进行配置 <servlet> <servlet-n ...