【问题标题】:How to add item(s) to a list box from a drop down如何从下拉列表中添加项目)
【发布时间】:2019-01-30 17:42:55
【问题描述】:

我正在尝试将下拉列表(从 sql server 填充的列表)中的项目添加到 asp.net 中的列表框(网络表单)。第一项添加没有问题,但就是这样。从下拉列表中选择第二项时,没有任何反应。这是下拉菜单的 SelectedIndexChanged 事件中使用的代码:

下拉列表和列表框都将 AutoPostBack 设置为 true。还需要做什么?不应该这么简单吗:

 protected void DropDownList_SelectedIndexChanged(object sender, 
 EventArgs e)
    {
        listBox.Items.Add(DropDownList.SelectedItem);
    }

                <asp:DropDownList ID="DropDownList" runat="server" 
EnableViewState="true" AutoPostBack="true"     
OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" 
Visible="true"></asp:DropDownList>


<asp:ListBox runat="server" ID="listBox" CssClass="form-control" 
AutoPostBack="true" EnableViewState="true"></asp:ListBox>

我需要将每个被选中的项目都添加到列表框中——而不仅仅是第一个项目。

【问题讨论】:

    标签: c# asp.net .net webforms listbox


    【解决方案1】:

    在您的事件中将文本转换为字符串。

    protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
       listBox.Items.Add(DropDownList.SelectedItem.ToString());
    }
    

    或者如果您有任何其他问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 2015-06-24
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 2013-06-11
      • 2014-03-14
      相关资源
      最近更新 更多