asp.net DropDownList使用
2013-12-15 10:04
169 查看
一、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;
}
相关文章推荐
- ASP.NET MVC中DropDownList的使用
- 详解ASP.NET MVC中DropDownList的使用
- 在ASP.NET MVC中使用DropDownList
- Scott Mitchell 的ASP.NET 2.0数据教程之三十三:: 使用DropDownList过滤的主/从报表
- Scott Mitchell 的ASP.NET 2.0数据操作教程之八:使用两个DropDownList过滤的主/从报表
- ASP.NETmvc5下使用DropDownList的使用 ,和asp.net里的DropDownList控件的方法
- Scott Mitchell 的ASP.NET 2.0数据教程之三十三:: 使用DropDownList过滤的主/从报表
- asp.net mvc dropdownlist使用中注意的一点
- 在ASP.NET MVC中使用DropDownList
- [备用]权限设计方案、如何使用session、MVC如何使用模板、DropdownList、怎么添加Bootstrape框架、使用ASP.NET MVC 4 Bootstrap Layout Template(VS2012)
- ASP.NET MVC中DropDownList的使用
- ASP.NET MVC中DROPDOWNLIST的使用
- Scott Mitchell 的ASP.NET 2.0数据教程之三十三:: 使用DropDownList过滤的主/从报表
- 详解ASP.NET MVC中DropDownList的使用
- ASP.NET中 DropDownList+FormView(表单视图)的使用前台绑定
- ASP.NET 2.0中的数据操作之八:使用两个DropDownList过滤的主/从报表
- asp.net的DropDownList的使用
- Scott Mitchell 的ASP.NET 2.0数据操作教程之七:使用DropDownList过滤的主/从报表
- Scott Mitchell 的ASP.NET 2.0数据教程之三十三:: 使用DropDownList过滤的主/从报表