DropDownList的用法

摘要:
DropDownList是一个下拉列表菜单,平时我们也会经常用到,下面就来看看如何绑定值1˃静态添加,就是说在值都很明确的情况下ListItemlist1=newListItem("a","1");ListItemlist2=newListItem("b","2");ListItemlist3=newListItem("c","3");ListItemlist4=newListItem("d","

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 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>动态绑定

@ 视图绑定

DropDownList的用法第1张

<asp:DropDownList runat="server"

DataTextField="Name" DataValueField="Id">

</asp:DropDownList>

<asp:ObjectDataSource runat="server"

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

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

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

@代码绑定

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

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

this.DropDownList1.DataBind();

具体有两种方法:

1ListItem 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);
}

2IList<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的用法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇齐二TK6916/20/26/32系列数控落地铣镗床简介2数据库性能优化一:数据库自身优化(大数据量)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

asp 获取url 返回值 和 对json 返回值的处理

1 Function GetHttpPage(HttpUrl,endoce) 2 If endoce = "" Then endoce = "GB2312" 3 If IsNull(HttpUrl)=True Or Len(HttpUrl)<18 Or HttpUrl="$False$"Then 4 GetHttpPage="$False$"...

WinForm GDI+自定义控件总结(一)

前言   由于项目的原因好久没写博客了,也正是项目的原因开始系统的学习WinForm,从而接触到自定义控件的开发。自定义控件的开发有一定的难度,对开发者要求比较高,需要了解Windows运行的机制,熟悉win32Api和GDI+。下面是我收集的一些资料,挺不错的。 资料 .NET组件编程http://www.cnblogs.com/mapserver/ca...

开源cms介绍:We7开源cms系统1

自从We7 CMS第一次走进大众的视野,就注定了它在这个舞台的与众不同的表现;辞旧迎新,跨越技术与用户体验的双重障碍,迎面而来的是全新的功能,优化的设计。这一次,We7 CMS 2.5版跳得台阶有点大。看看2.5版有什么不同?     1. 插件功能:是它使We7如此与众不同 开放插件成就了Firefox,成就了Wordpress! 今天,We7创造了...

sqlite3 语法

https://www.sqlite.org/index.html  官网 http://tutlane.com/tutorial/sqlite/sqlite-case-statement     http://www.runoob.com/sqlite/sqlite-data-types.html http://www.w3school.com.cn/s...

处理ASP.NET 请求(IIS)

原文:http://www.cnblogs.com/hkncd/archive/2012/03/23/2413917.html 英文原文:Beginner’s Guide: How IIS Process ASP.NET Request 前言   每次服务器接受到请求,都要先经IIS处理。这不是一篇描述ASP.NE生命周期的文章,仅仅是关于IIS操作的。在...

GridView嵌套实现

  GridView嵌套实现 效果图: 技术实现: 前台: <%@PageLanguage="C#"AutoEventWireup="true" CodeFile="Default.aspx.cs"Inherits="_Default"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transit...