发布于 2016-06-09 05:36:40 | 148 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


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:Listitem> 
<asp:Listitem value="1">男</asp:Listitem> 
<asp: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) 
{ 
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:Listitem value="1">男</asp:Listitem> 取1
.net中 DropDownList1.SelectedValue.ToString()
javascirpt var ddl1=document.getElementByIdx_x("DropDownList1").selectedIndex;
2.2 取DropDownList1的选项,也就是选择item值<asp:Listitem value="1">男</asp:Listitem> 取 男
.net 中DropDownList1.SelectedItem.ToString();
javascript document.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即可。
例如:如果专业后的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; 
} 



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务