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/
hostkvm在2021年3月新上线洛杉矶新VPS业务,强制三网接入中国联通优化线路,是当前中美之间性价比最高、最火热的线路之一,性价比高、速度非常好,接近联通AS9929和电信AS4809的效果,带宽充裕,晚高峰也不爆炸。 官方网站:https://hostkvm.com 全场优惠码:2021(全场通用八折,终身码,长期) 美国 US-Plan0【三网联通优化线路】 内存:1G CPU:...
rangcloud怎么样?rangcloud是去年年初开办的国人商家,RangCloud是一家以销售NAT起步,后续逐渐开始拓展到VPS及云主机业务,目前有中国香港、美国西雅图、韩国NAT、广州移动、江门移动、镇江BGP、山东联通、山东BGP等机房。目前,RangCloud提供香港CN2线路云服务器,电信走CN2、联通移动直连,云主机采用PCle固态硬盘,19.8元/月起,支持建站使用;美国高防云...
前些天赵容分享过DogYun(狗云)香港BGP线路AMD 5950X经典低价云服务器的信息(点击查看),刚好账户还有点余额够开个最低配,所以手贱尝试下,这些贴上简单测试信息,方便大家参考。官方网站:www.dogyun.com主机配置我搞的是最低款优惠后14.4元/月的,配置单核,512MB内存,10GB硬盘,300GB/50Mbps月流量。基本信息DogYun的VPS主机管理集成在会员中心,包括...