DropDownList的用法
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的用法的更多相关文章
- Html.DropDownList的用法
直接上代码 页面代码 <td> <%= Html.DropDownList("selCity") %> </td> controller里面的代 ...
- Html.DropDownList()的用法
页面代码如下: <%= Html.DropDownList("Category", ViewData["Categories"] as SelectLis ...
- [转]Html.DropDownList()的用法 ( Asp.Net MVC)
Html.DropDownList()赋默认值: 页面代码如下: <% List<SelectListItem> list = new List<SelectListItem& ...
- DropDownList和GridView用法
DropDownList和GridView用法 DropDownList控件和GridView控件在Asp.net中相当常用,以下是控件的解释,有些是常用的,有些是偶尔的,查找.使用.记录,仅此而 ...
- [摘]在ASP.NET MVC中使用DropDownList
在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便的办法还是使用HtmlHelper中的辅助方法.在View中,包含一个类型为HtmlHelper的属性H ...
- ASP.NET MVC中使用DropDownList
在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便的办法还是使用HtmlHelper中的辅助方法.在View中,包含一个类型为HtmlHelper的属性H ...
- GridView的常规用法
GridView控件在Asp.net中相当常用,以下是控件的解释,有些是常用的,有些是偶尔用到的,查找.使用.记录,仅此而已.(最后附带DropDownList控件) ASP.NET中GridView ...
- 在ASP.NET MVC中使用DropDownList
在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便的办法还是使用HtmlHelper中的辅助方法.在View中,包含一个类型为HtmlHelper的属性H ...
- <转>ASP.NET学习笔记之在ASP.NET MVC中使用DropDownList
看到一篇关于dropdownlist的用法很好的阐述,比较清楚,留着,防止以后自己不记得,还可以瞅瞅. 在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便 ...
随机推荐
- linux 标准输入输出
文件描述符是一个简单的正整数,用以标明每一个被进程所打开的文件和socket.最前面的三个文件描述符(0,1,2)分别与标准输入(stdin),标准输出(stdout)和标准错误(stderr)对应 ...
- STM32中断控制及优先级设置
M3用8bits而STM32用高四位来表示抢占和子优先级:bit=1表示抢占:bit=0表示非抢占即子优先级:所以共有5中方案分组: 分组 Bit7 Bit6 Bit5 Bit4 说明: 第0组 ...
- sublime相关设置
1.设置Sublime Text新标签页打开文件 "open_files_in_new_window": false,
- JAVA 99乘法表实例
实例: public class Test{ public static void main(String[] args){ for(int i=1;i<=9;i++){ for(int j=1 ...
- Python进阶05 循环设计
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在"循环"一节,我们已经讨论了Python基本的循环语法.这一 ...
- windows timeGetTime() 函数 获取系统从开机到现在的毫秒时间值
#include <windows.h> #include <iostream> #pragma comment( lib,"winmm.lib" ) in ...
- windowns--HANDLE,
HANDLE: 在windows程序中,有各种各样的资源(窗口.图标.光标等),系统在创建这些资源时会为他们分配内存,并返回标示这些资源的标示号,即句柄. 句柄指的是一个核心对象在某一个进程中的唯一索 ...
- 《一课经济学》书摘笔记II
假设有位制衣商了解到,有种机器可以用以往一半的人力生产男式和女式大衣.于是,他购置了这种机器,并且裁掉了一半的员工.这位制衣商由于节省开支而获得了以前没有的利润.他从制衣工人直接工资那里节省下来的每一 ...
- 502 Bad Gateway什么意思
http://baike.baidu.com/link?url=U2ijg5T5PG_tTkY67mqfx07co7qGqvMB32rbLwq4S2ThBSRIWWvU76Y0Mb8Z3z6nbViN ...
- MySQL主存复制与读写分离的感悟
1.主存复制: 就是实现数据拷贝,有点实时的感觉,完成数据同步,存储两份数据. 项目开发中,类似场景许多,尤其是异构系统之间的交互,协作.-------------------场景目的:为了安全,各自 ...