DropDownList
2015-11-26 10:21
344 查看
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>代码绑定
这种绑定方式非常灵活,也能很好的解决上面的问题,下面就来看看如何绑定
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>");
}
灵活掌握就可以了
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>代码绑定
这种绑定方式非常灵活,也能很好的解决上面的问题,下面就来看看如何绑定
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>");
}
灵活掌握就可以了
相关文章推荐
- Debug模式启动tomcat超时
- Nginx服务器的功能模块
- linux rm文件恢复总结
- CentOS学习9_CentOS安装系统时硬盘分区建议
- 【学神-RHEL7】1-3-Linux基本命令和配置服务器来电后自动开机
- 图示-Centos7完整安装
- 修改 linux 默认启动级别 设置系统时间 定时关机
- 各种网站内容收集
- mac 的cornerstone在update时提示the working copy is locked
- 网站压力测试webbeach的安装使用
- docker 使用 iso虚拟光驱一键式安装
- Linux上安装SVN Server 服务器的方法
- hadoop存储测试
- CentOS下安装配置LAMP(Linux+Apache+MySQL+PHP)
- 网站日志分析
- linux 常用命令
- 修改 linux 默认启动级别 设置系统时间 定时关机
- Windows 和 Linux 下 禁止ping的方法
- 修改 linux 默认启动级别 设置系统时间 定时关机
- 修改