ASP.NET中怎样让下拉框控件默认值为空?

来源:百度知道 编辑:UC知道 时间:2024/07/11 19:04:18
我用Visul Studio2005设计网站,DorpDownList控件总是默认显示数据库中的第一条记录。

怎么样让网页打开时,下拉框默认显示空啊?

谢谢大家!!
请大家说清楚一点好吗?要在哪里写?下拉框的SelectedIndexChanged事件中吗?

貌似好改,实际也不好改,因为如果用新增空条目的办法,新增的条目是在最后一条,即使默认选中也是排在最后面,用户还要手动拉滚动条才能看到前面的条目,不合常理。

我又想了一下,实际有一个很简单的办法:

在dropdownlist中加入一个AppendDataBoundItems="True"的属性,然后设置一条静态条目,内容为空,当然也可以设置成“请选择数据”之类的值。

具体如下

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" >
<asp:ListItem Value="0">Please Select</asp:ListItem>
</asp:DropDownList>

这样绑定的数据只会追加到这条静态数据之后,而默认选中的是这条静态数据。

DropDownList.Items.Add("","");//添加空条目

DropDownList.SelectValue = "";//将DropDownList的选择框赋值。

PS:这里的DropDownList是该控件的ID。

<asp:DropDownList ID="DropDownList1" runat="server" Width="131px">
<asp:ListItem Selected=True></asp:ListItem>
<asp:ListItem Value="0" >客户编号</asp:ListItem>
<asp:ListItem Value="1&q