12.14dropdownlist下拉框
2015-12-15 14:13
295 查看
dropdownlist 下拉列表
if(!IsPostBack)//不是提交返回 是 第一次打开的 大多数load页面都要写次句话
{}
l
1 绑定数据
1: NationDataContext context = new NationDataContext();
//指定了数据源
DropDownList1.DataSource = context.Nation; //将natiion 表中的数据给下拉表
DropDownList1.DataValueField = "Code";//表示后台的值相当于valuemembers,相当于那个字段
DropDownList1.DataTextField = "Name";//表示要显示的值是哪一个字段 相当于displaymembers
//绑定数据
DropDownList1.DataBind();
2:可以用于数据追加
NationDataContext context = new NationDataContext();
ListItem item = new ListItem();
item.Text = "中国";
item.Value = "0001";
ListItem item1 = new ListItem();
item1.Text = "韩国";
item1.Value = "0001";
DropDownList1.Items.Add(item);
DropDownList1.Items.Add(item1);
3 :手动绑定数据
2:取选中的值 AutoPostBack 设置为true
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedValue.ToString();
}
3:设置那一项默认选中
方式一:
DropDownList1.SelectedIndex = 2;
方式二:
foreach(ListItem item in DropDownList1.Items)
{
if(item.Value=="2")
{
item.Selected = true;
}
}
4 练习:三级联动
if(!IsPostBack)//不是提交返回 是 第一次打开的 大多数load页面都要写次句话
{}
l
1 绑定数据
1: NationDataContext context = new NationDataContext();
//指定了数据源
DropDownList1.DataSource = context.Nation; //将natiion 表中的数据给下拉表
DropDownList1.DataValueField = "Code";//表示后台的值相当于valuemembers,相当于那个字段
DropDownList1.DataTextField = "Name";//表示要显示的值是哪一个字段 相当于displaymembers
//绑定数据
DropDownList1.DataBind();
2:可以用于数据追加
NationDataContext context = new NationDataContext();
ListItem item = new ListItem();
item.Text = "中国";
item.Value = "0001";
ListItem item1 = new ListItem();
item1.Text = "韩国";
item1.Value = "0001";
DropDownList1.Items.Add(item);
DropDownList1.Items.Add(item1);
3 :手动绑定数据
2:取选中的值 AutoPostBack 设置为true
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedValue.ToString();
}
3:设置那一项默认选中
方式一:
DropDownList1.SelectedIndex = 2;
方式二:
foreach(ListItem item in DropDownList1.Items)
{
if(item.Value=="2")
{
item.Selected = true;
}
}
4 练习:三级联动
相关文章推荐
- 关于Linux的Jiffies/Tick/HZ
- Linux每日学习(二)
- Linux makefile 文件
- OpenGL中FBO的概念及其应用 [转]
- JVM虚拟机---在centos下安装及编译openjdk
- Properties读取配置文件
- 16个Linux服务器监控命令
- MAC OS下使用OpenSSL生成私钥和公钥的方法
- 【转】开源性能测试工具 - Apache ab 介绍
- Apache与Nginx的比较
- linux学习教材(七)vi编
- shell脚本连接、读写、操作mysql数据库实例
- VMware中CentOS设置静态IP
- 手机网站打电话链接等
- centos7 安装最新docker 1.9.1
- 解决org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource
- 龙珠直播CTO谭振林:从0到500万用户背后的技术架构变迁
- Docker容器、镜像、数据卷
- Shell脚本8种字符串截取方法总结
- 欢迎使用CSDN-markdown编辑器