【问题标题】:Dynamically add items to DropDownList but still allow for a field that has no value?将项目动态添加到 DropDownList 但仍然允许没有值的字段?
【发布时间】:2012-01-30 15:05:10
【问题描述】:

我已经用数据库中不同的项目列填充了我的 DropDownLists,但我正在尝试使列表中的第一个项目没有值,其文本类似于 “选择一个项目”

由于某种原因,即使我将项目添加到列表中并使其成为选定项目,它也会被数据库中的项目覆盖...

我怎样才能做到这一点?

更新:

一切都由 .aspx 页面设计器完成,但这里是生成的代码-

          <asp:DropDownList ID="ddlUnits1" runat="server" DataSourceID="UnitsEDS" 
                   DataTextField="unitId" DataValueField="unitId">
                   <asp:ListItem Selected="True">Select Units</asp:ListItem>
          </asp:DropDownList>

          <asp:EntityDataSource ID="UnitsEDS" runat="server" 
                   ConnectionString="name=UnitsEntity" 
                   DefaultContainerName="UnitsEntity" EnableFlattening="False" EntitySetName="spillunits">
          </asp:EntityDataSource>

【问题讨论】:

  • 您有一些示例代码供我们查看吗?

标签: asp.net webforms drop-down-menu web-controls


【解决方案1】:

使用 ListControl.AppendDataBoundItems 属性:

AppendDataBoundItems Documentation

来自文档:“AppendDataBoundItems 属性允许您在数据绑定发生之前将项目添加到 ListControl 对象。数据绑定之后,项目集合包含来自数据源的项目和先前添加的项目。”

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2022-11-11
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多