DropDownList是一个下拉列表菜单,平时我们也会经常用到,下面就来看看如何绑定值

1>     静态添加,就是说在值都很明确的情况下

ListItem list1 = new ListItem("a","1");

ListItem list2 = new ListItem("b", "2");

ListItem list3 = new ListItem("c", "3");

ListItem list4 = new ListItem("d", "4");

this.DropDownList2.Items.Add(list1);

this.DropDownList2.Items.Add(list2);

this.DropDownList2.Items.Add(list3);

this.DropDownList2.Items.Add(list4);

如果是要特别放置,明确每个的具体索引,我们可以采用insert方法添加

This.DropDownList2.Items.insert(int index,Listitem item)

也可以在后台html直接添加:

<asp:DropDownList ID="DropDownList2" runat="server"  >

<asp:ListItem Value="1">a</asp:ListItem>

<asp:ListItem Value="2">b</asp:ListItem>

<asp:ListItem Value="3">c</asp:ListItem>

<asp:ListItem Value="4">d</asp:ListItem>

</asp:DropDownList>

转为html就是

<select id="select">

<option value="1">a</option>

<option value="2">a</option>

<option value="3">a</option>

<option value="4">a</option>

</select>

2>     动态绑定

@ 视图绑定

<asp:DropDownList ID="DropDownList1" runat="server"

DataTextField="Name" DataValueField="Id">

</asp:DropDownList>

<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"

OldValuesParameterFormatString="original_{0}" SelectMethod="GetAllUser"

TypeName="MyBookShop.BLL.UserManager"></asp:ObjectDataSource>

这样绑定唯一不好的一点就是无法再第一个列表中显示“—请选择—”

@  代码绑定

这种绑定方式非常灵活,也能很好的解决上面的问题,下面就来看看如何绑定

this.DropDownList1.DataSource = UserManager.GetAllUser();

this.DropDownList1.DataBind();

具体有两种方法:

1   ListItem list = new ListItem("--请选择--","0");

this.DropDownList1.DataSource = UserManager.GetAllUser();

this.DropDownList1.Items.Insert(0, list);

this.DropDownList1.DataTextField = "name";

this.DropDownList1.DataValueField = "id";

this.DropDownList1.DataBind();

但是当我绑定是DataSet的时候,上面那样写的话就没有加上("--请选择--"),这个要放在下面写才可以,如

protected void LinkBind()
    {
        DataSet ds = linkManager.GetList(" jh_checked='是'");
        DroUrl.DataSource = ds;
        DroUrl.DataTextField = "jh_linkname";
        DroUrl.DataValueField = "jh_linkurl";
        DroUrl.DataBind();
        ListItem item = new ListItem("--请选择---", "#");
        DroUrl.Items.Insert(0,item);
    }

2      IList<DepartInfo> departList = DepartInfoManager.GetDepartList();

departList.Insert(0, new DepartInfo { DepartId = 0, DepartName = "--请选择--" });

ddDepart.DataSource = departList;

ddDepart.DataTextField = "DepartName";

ddDepart.DataValueField = "DepartId";

ddDepart.DataBind();

平时我们会在友情链接中使用,选择一项的时候链接会改变,而且打开一个新的页面,这个很好实现

protected void DropDownList1_TextChanged(object sender, EventArgs e)
       {
        string link = this.DropDownList1.SelectedValue;
        Response.Write("<script>window.open('"+link+"','_Blank')</script>");
       }

灵活掌握就可以了

DropDownList的用法的更多相关文章

  1. Html.DropDownList的用法

    直接上代码 页面代码 <td> <%= Html.DropDownList("selCity") %> </td> controller里面的代 ...

  2. Html.DropDownList()的用法

    页面代码如下: <%= Html.DropDownList("Category", ViewData["Categories"] as SelectLis ...

  3. [转]Html.DropDownList()的用法 ( Asp.Net MVC)

    Html.DropDownList()赋默认值: 页面代码如下: <% List<SelectListItem> list = new List<SelectListItem& ...

  4. DropDownList和GridView用法

    DropDownList和GridView用法   DropDownList控件和GridView控件在Asp.net中相当常用,以下是控件的解释,有些是常用的,有些是偶尔的,查找.使用.记录,仅此而 ...

  5. [摘]在ASP.NET MVC中使用DropDownList

    在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便的办法还是使用HtmlHelper中的辅助方法.在View中,包含一个类型为HtmlHelper的属性H ...

  6. ASP.NET MVC中使用DropDownList

    在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便的办法还是使用HtmlHelper中的辅助方法.在View中,包含一个类型为HtmlHelper的属性H ...

  7. GridView的常规用法

    GridView控件在Asp.net中相当常用,以下是控件的解释,有些是常用的,有些是偶尔用到的,查找.使用.记录,仅此而已.(最后附带DropDownList控件) ASP.NET中GridView ...

  8. 在ASP.NET MVC中使用DropDownList

    在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便的办法还是使用HtmlHelper中的辅助方法.在View中,包含一个类型为HtmlHelper的属性H ...

  9. <转>ASP.NET学习笔记之在ASP.NET MVC中使用DropDownList

    看到一篇关于dropdownlist的用法很好的阐述,比较清楚,留着,防止以后自己不记得,还可以瞅瞅. 在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便 ...

随机推荐

  1. 转--Android实用的代码片段 常用代码总结

    这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下     1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...

  2. Spring中的AOP应用

    AOP被称为面向切面编程,AOP中的几个重要概念是: 1.切面.切面就是要实现的功能.切面通常是在多数方法中会用到的相同功能,如写日志. 2.连接点.连接点就是应用程序执行过程中插入切面的地点.如:方 ...

  3. wireshark1.8捕获无线网卡的数据包——找不到无线网卡!

    问题说明:奇怪的是,我线网卡明明有的,是interl的型号,可是wireshark总是找不到,如下: 奇了怪了,没有!原来是如下的: 实际上这块无线网卡是存在的,只不过由于兼容或驱动的原因无法显示型号 ...

  4. 如何使用XAMPP本地搭建一个属于你自己的网站

    你好,从今天开始,我将为大家带来一些我学习SEO和建站的免费教程,今天为大家带来的是如何用XAMPP搭建一个属于你自己的网站.来到这里,可以说很多在百度上已经过时了的资料需要总结的资料这里都有,你只要 ...

  5. [ActionScript 3.0] LocalConnection示例

    下例包含两个 ActionScript 类,这两个类应当编译到两个单独的 SWF 文件中: 在 LocalConnectionSenderExample SWF 文件中,将创建 LocalConnec ...

  6. DimDate

    CREATE TABLE [dbo].[DimDate]( [DateKey] int NOT NULL , [FullDate] DATE NOT NULL , [MonthNumberOfYear ...

  7. Ext 项目随笔

    region: This region's layout position (north, south, east, west or center). Read-only. collapsible:t ...

  8. Matla学习:figure+axes+plot

    function fig = SetDrawParam() %.获得屏幕尺寸 figpos = , 'ScreenSize');%获得屏幕尺寸,单位像素 %.设置坐标系在画布中的位置,针对不同尺寸或不 ...

  9. (medium)LeetCode 222.Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  10. 解决oralce 11g dg搭建报错:ORA-16664、ORA-16714、ORA-16810问题--转

    下面不是小编错误报告只是转了网络一篇,同时也解决了我的问题所以复制过来给各位参考. 最近在弄11g的dg时,遇到如下问题,记录下.首先在主上查看报如下错误: DGMGRL> show confi ...