【问题标题】:Why is my DropDownList outputting the wrong value for the first list item only?为什么我的 DropDownList 只为第一个列表项输出错误的值?
【发布时间】:2012-02-18 23:15:06
【问题描述】:

我将数据上下文中的城市加载到两个不同的列表中,并将它们绑定到各自的 DropDownList 控件。

虽然两者的代码相同,只是数据和控件的名称不同,但数据绑定似乎只对其中一个不能正常工作。它不显示城市名称,而是显示其 Id,即仅针对第一个选项!

我们有两个 DropDownList 控件:

  1. DestinationDropDownList;
  2. OriginDropDownList

然后,我填充它们。

public partial class MyControl : UserControl {
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            var instruction = new City() {
                CityId = Guid.Empty,
                CityName = "- Select a city -"
            };

            var destinations = context.Cities.ToList();
            destinations = destinations.OrderBy(c => c.CityName).ToList();
            destinations.Insert(0, instruction);
            DestinationDropDownList.DataSource = destinations;
            DestinationDropDownList.DataTextField = "CityName";
            DestinationDropDownList.DataValueField = "CityId";
            DestinationDropDownList.DataBind();

            var origins = context.Cities.ToList();
            origins = origins.OrderBy(c => c.CityName).ToList();
            origins.Insert(0, instruction);
            OriginDropDownList.DataSource = origins;
            OriginDropDownList.DataTextField = "CityName";
            OriginDropDownList.DataValueField = "CityId";
            OriginDropDownList.DataBind();
        }
    }
    private static readonly MyDataContext context = new MyDataContext();
}

HTML:

<asp:DropDownList ID="DestinationDropDownList" runat="server" />
<asp:DropDownList ID="OriginDropDownList" runat="server" />

显示的数据:

*DestinationDropwDownList*
    - 0000-0000-0000-0000-0000 (empty Guid)
    - CityName 01
    - CityName 02
    - ...

*OriginDropDownList*
    - - Select a city -
    - CityName 01
    - CityName 02
    - ...

正确的显示是由OriginDropDownList 控件呈现的。为什么DestinationDropDownList 没有正确显示数据,即列表中的第一个也是唯一的第一项?

  1. 我尝试在插入指令之前删除第一项;
  2. 我尝试插入指令两次,然后删除第一个;
  3. 我尝试使用 DropDownList.Items.Add(string) 方法,但第一行没有显示任何内容,而是显示空的 Guid。

我该如何纠正这种行为!?

【问题讨论】:

  • 我无法重现此问题...
  • JonH:我很高兴你不能这样做,因为当它发生时真是太痛苦了! ;) 谢谢你的一粒盐! =)
  • 我只是不明白为什么会发生在你身上......
  • DestinationDropDownList.DataBind() 上设置断点并调试您的应用程序。执行该行后,DestinationDropDownList.Items[0].Text 的值是多少?
  • Micheal: DestinationDropDownList.Items[0].Text == "- Choisir -",所以显示应该没问题。另外,当我对其进行快速监视时,DestinationDropDownList.Text == Guid.Empty,这就是显示的内容,而不是实际选定的项目。有什么想法吗?

标签: asp.net data-binding webforms drop-down-menu


【解决方案1】:

我以前没有遇到过这个问题,但我会尝试回答你的第二个问题。我认为通过代码添加第一个“指导性”项目没有什么价值(如果我错了,请纠正我)。我宁愿直接将它添加到标记中:

<asp:DropDownList ID="DestinationDropDownList" runat="server"
  AppendDataBoundItems="true"
  DataTextField="CityName"
  DataTextField="CityId">
  <asp:ListItem Text="- Select a city -" />
</asp:DropDownList>

由于使用了AppendDataBoundItems 属性,第一项将在数据绑定中保留下来。我还添加了DataTextFieldDataValueField 属性,您也可以从代码中删除它们。

在数据绑定之前您可以做的另一件事是将实体投影到ListItem 实例:

DestinationDropDownList.DataSource = 
  from d in destinations
  select new ListItem() {
    Text = d.CityName,
    Value = d.CityId.ToString()
  };
DestinationDropDownList.DataBind();

这样,您不必在控件上设置DataTextFieldDataValueField 属性,因为您已经在自己创建列表项。

【讨论】:

  • 我不知道那个!让我试一试,看看会发生什么。 =) 谢谢!
  • +1 教我一种我不知道的方式。 - 添加第一个ListItem 效果很好,因为即使在数据绑定发生后我也可以看到该项目。但是,仅对于第一项,显示的是它的值,所以我会继续看到 Guid.Empty 值。看起来它只对列表中的第一项执行此操作,因为我可以从我插入代码中的那一项中看到我的指令。
  • 至于我通过代码实现它的观点,只是因为这是在 Windows 窗体中实现它的方式。我是 Web 开发的初学者,所以我还不是很熟悉这些 HTML 和 ASP.NET 标记和属性。我宁愿看 C# 代码,我的印象是它可以让我更好地控制控件以及整个页面中发生的事情。 =)
  • @WillMarcouiller 你是说即使在第一个项目的标记中设置了Text 属性,它仍然显示0000-0000 ...?我已经对代码进行了更多调整,看看是否有帮助。
  • 我实际上使用了你的第一项数据绑定方式,将ListItem.Value属性设置为"- Select a city -"。我从代码中删除了指令插入,结果正是我想要的。不幸的是,我只是还不明白这是怎么发生的。我们找到了一种解决方法,但这不是真正的解决方案,因为您的确切代码应该可以解决问题。反正!感谢您的热心帮助!
猜你喜欢
  • 1970-01-01
  • 2022-07-29
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多