日期:2014-05-18  浏览次数:20500 次

DropDownList 控件清空时,每个选项能否保留
在load事件下,我的代码如下:

DataView dv1 = new DataView(idt);
  dv1.RowFilter = ("parentcode='10'");
  txtha03.DataSource = dv1;
  txtha03.DataTextField = "cinvcname";
  txtha03.DataValueField = "no";
  txtha03.DataBind();

txtha03的下拉框值是A,B,C

我在一个按钮里添加如下事件,目的是暂时让 txtha03 当前text的值为"",方便查询取值

this.txtha03.SelectedItem.Text = "";

结果发现txtha03.当前的值的确为"",但是下拉框的值却变成了空白,B,C,少了一个选项.

我想让下拉框清掉当前值,但是下拉框的下拉选项也不会减少,代码该如何改呢


------解决方案--------------------
DropDownList1.Items.Insert(0, new ListItem("请选择...", ""));
------解决方案--------------------
this.txtha03.SelectedItem.Text = "";
你把A设置为""


添加默认值
DropDownList1.Items.Insert(0, new ListItem("请选择...", ""));
------解决方案--------------------

HTML code


<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
        <asp:ListItem Text="" Value="">
        </asp:ListItem>
        <asp:ListItem Text="A" Value="A">
        </asp:ListItem>
        <asp:ListItem Text="B" Value="B">
        </asp:ListItem>
        <asp:ListItem Text="C" Value="C">
        </asp:ListItem>
        </asp:DropDownList>

------解决方案--------------------
AppendDataBoundItems 就是是否在原来项的基础上来增加

MSDN上面搜搜啊。

探讨
引用:
HTML code


<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="">
</asp:ListItem>
<asp:ListItem Text="A" Value="A……