http://taskcn.blog. 163.com/
ASP.NET MVC DropDownList数据绑定及使用详解DropDownList控件用于创建下拉列表。 DropDownList控件中的每个可选项都是由ListItem元素定义的!该控件支持数据绑定
一 DropDownList
1. 1 DropDownList绑定数据
1. 1. 1 DropDownList固定绑定
这种方式适合那些已经固定的数据绑定到DropDownList上。
例
复制代码代码如下:asp:DropDownList runat="server" ID="ddlArea" Width="120px"asp:Listitem value="0"选择性别/asp:Listitemasp:Listitem value="1"男/asp:Listitemasp:Listitem value="2"女/asp:Listitem
/asp:DropDownList
1. 1.2 DropDownList动态绑定
前台
后台两种方法 注意每次绑定都要清除一下原来的记录例ddlArea. Items.Clear() ;
第一种
复制代码代码如下:
SqlConnection conn = new
SqlConnection("server=. ;uid=sa;database=pubs") ;
SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn) ;DataTable dt = new DataTable() ;dap.Fill (dt) ;
DropDownList1. Items.Clear() ;
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "job_desc";
DropDownList1.DataValueField = "job_id";
DropDownList1.DataBind() ;
DropDownList1. Items. Insert(0, new ListItem("选择数据", "随机绑定") ) ;//插入默认项此举必须放到数据绑定之后效果
第二种
复制代码代码如下:
SqlConnection conn = new
SqlConnection("server=. ;uid=sa;database=pubs") ;
SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn) ;DataTable dt = new DataTable() ;dap.Fill (dt) ;if (dt.Rows.Count != 0)
{http://taskcn.blog. 163.com/
http://taskcn.blog. 163.com/
DropDownList1. Items.Clear() ;for (int i = 0; i dt.Rows.Count; i++)
{
DropDownList1. Items.Add(new ListItem(dt.Rows[i] ["显示值"] .ToString() ,dt.Rows[i] ["usbkey"] .ToString() ) ) ;
}
DropDownList1. Items. Insert(0, "选择网吧") ;
DropDownList1. Items[0] .Value = "0";或
// DropDownList1. Items. Insert(0, new ListItem("选择数据", "随机绑定") ) ;//插入默认项此举必须放到数据绑定之
}else
{
DropDownList1. Items. Insert(0, "无网吧记录") ;
DropDownList1. Items[0] .Value = "0";
}
二 DropDownList1的取值问题
2. 1取DropDownList1的索引值也就是选择value值asp:Listitemvalue="1"男/asp:Listitem取1
.net中DropDownList1.SelectedValue.ToString()javascirpt varddl1=document.getElementByIdx_x("DropDownList1") . selectedIndex;
2.2取DropDownList1的选项也就是选择item值asp:Listitem value="1"男/asp:Listitem取男
.net中DropDownList1.SelectedItem.ToString() ;javascriptdocument.getElementByIdx_x("DropDownList1") .options[document.getElement("selectID") . selectedIndex] .value
三 DropDownList1事件问题
重点:使用OnTextChanged,OnSelectedIndexChanged事件时必须设置复制代码代码如下:asp:DropDownList runat="server"
OnTextChanged="DropDownList1_TextChanged"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1"
OnTextChanged,OnSelectedIndexChanged这两个事件具体有什么区别我也没测试出来只知道OnSelectedIndexChanged这个事件要比OnTextChanged执行的早也就是如果这两个事件都存在会首先执行OnSelectedIndexChanged这个事件然后才执行OnTextChanged.
四如何避免DropDownList下拉框中的值重复添加
AppendDataBoundItems是否填加重复值。真为添加假为不填加
原因 DropDownList控件AppendDataBoundItems属性设置为"True"了的改为False即可。http://taskcn.blog. 163.com/
http://taskcn.blog. 163.com/
例如如果专业后的DropDownList控件AppendDataBoundItems属性设置为"True"那么选择院系后专业里的值会不断添加。
五区别
复制代码代码如下:depart_ddl. Items. Insert(0,new ListItem("不选该项", "0") ) ;这是在首项添加数据。
Items.Add是在最后添加
DropDownList1. Items.Add(new ListItem("Text", "value") ) ;是在最后添加DropDownList1. Items. Insert(Index,new ListItem("Text", "value") ) ;这是在首项添加数据。
六从数据库中读取数据并绑定到DropDownList中
复制代码代码如下:if (ds.Tables[0] .Rows[0] ["State"] .ToString ()=="True")
{
DropDownListState. Items.FindByValue("1") .Selected =true;
}else
{
DropDownListState. Items.FindByValue("0") .Selected =true;
}http://taskcn.blog. 163.com/
3C云互联怎么样?3C云互联专注免备案香港美国日本韩国台湾云主机vps服务器,美国高防CN2GIA,香港CN2GIA,顶级线路优化,高端品质售后无忧!致力于对互联网云计算科技深入研发与运营的极客共同搭建而成,将云计算与网络核心技术转化为最稳定,安全,高速以及极具性价比的云服务器等产品提供给用户!专注为个人开发者用户,中小型,大型企业用户提供一站式核心网络云端服务部署,促使用户云端部署化简为零,轻松...
Hostodo又发布了几款针对7月4日美国独立日的优惠套餐(Independence Day Super Sale),均为年付,基于KVM架构,采用NVMe硬盘,最低13.99美元起,可选拉斯维加斯或者迈阿密机房。这是一家成立于2014年的国外VPS主机商,主打低价VPS套餐且年付为主,基于OpenVZ和KVM架构,产品性能一般,支持使用PayPal或者支付宝等付款方式。商家客服响应也比较一般,推...
ftlcloud怎么样?ftlcloud(超云)目前正在搞暑假促销,美国圣何塞数据中心的云服务器低至9元/月,系统盘与数据盘分离,支持Windows和Linux,免费防御CC攻击,自带10Gbps的DDoS防御。FTL-超云服务器的主要特色:稳定、安全、弹性、高性能的云端计算服务,快速部署,并且可根据业务需要扩展计算能力,按需付费,节约成本,提高资源的有效利用率。点击进入:ftlcloud官方网站...