Asp.net 为什么我的DropDownList只为第一个列表项输出了错误的值?

Asp.net 为什么我的DropDownList只为第一个列表项输出了错误的值?,asp.net,data-binding,webforms,drop-down-menu,Asp.net,Data Binding,Webforms,Drop Down Menu,我将城市从数据上下文加载到两个不同的列表中,并将它们绑定到各自的DropDownList控件 虽然这两个控件的代码都是相同的,并且只有数据和控件的名称不同,但是数据绑定似乎不能为其中一个控件正常工作。它不显示城市名称,而是显示其Id,即仅用于第一个选项 我们有两个下拉列表控件: DestinationDropDownList OriginDropDownList 然后,我填充它们 public partial class MyControl : UserControl { protect

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

虽然这两个控件的代码都是相同的,并且只有数据和控件的名称不同,但是数据绑定似乎不能为其中一个控件正常工作。它不显示城市名称,而是显示其Id,即仅用于第一个选项

我们有两个下拉列表控件:

  • DestinationDropDownList
  • 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:

    正确的显示是由
    OriginDropDownList
    控件呈现的显示。为什么
    DestinationDropDownList
    不能正确显示数据,即列表中的第一项和唯一第一项的数据

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

  • 我怎样才能纠正这种行为

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

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

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

    我无法重现这个问题……JonH:我有点高兴您不能,因为发生这种情况时非常痛苦!;)谢谢你的支持!=)我真不明白为什么会发生这种情况……在
    DestinationDropDownList.DataBind()上设置一个断点,然后调试你的应用程序。执行该行后,
    DestinationDropDownList.Items[0]的值是多少?Text
    ?Micheal:
    DestinationDropDownList.Items[0]。Text=“-Choisir-”
    ,因此显示应该正常。另外,当我对它进行快速监视时,
    DestinationDropDownList.Text==Guid.Empty
    ,这是显示的内容,而不是实际选择的项目。知道吗?我不知道那件事!让我试试,看看会发生什么谢谢+谢谢你教我一种我不知道的方法添加第一个
    ListItem
    效果很好,因为即使在数据绑定发生之后,我也可以看到该项。但是,仅对于第一项,显示的是它的值,因此我将继续看到
    Guid.Empty
    值。看起来它只对列表中的第一项执行此操作,因为我可以从代码中插入的指令中看到我的指令。至于我要通过代码执行此操作的观点,只是因为这是在Windows窗体中执行此操作的方法。我是Web开发的初学者,所以我还不太熟悉这些HTML和ASP.NET标记和属性。我更喜欢看C代码,我的印象是它让我更好地控制控件以及整个页面中发生的事情@WillMarcouiller你是说即使在第一个项目的标记中设置了
    Text
    属性,它仍然显示0000-0000。。。?我对代码做了更多的调整,看看是否有帮助。我实际上使用了您对第一项的数据绑定方式,将
    ListItem.Value
    属性设置为
    “-选择一个城市-”
    。我从代码中删除了指令插入,结果正是我想要它做的。不幸的是,我还不明白这是怎么发生的。我们找到了一种解决方法,但这并不是真正的解决方案,因为您的确切代码应该可以做到这一点。无论如何谢谢你的帮助!
    *DestinationDropwDownList*
        - 0000-0000-0000-0000-0000 (empty Guid)
        - CityName 01
        - CityName 02
        - ...
    
    *OriginDropDownList*
        - - Select a city -
        - CityName 01
        - CityName 02
        - ...
    
    <asp:DropDownList ID="DestinationDropDownList" runat="server"
      AppendDataBoundItems="true"
      DataTextField="CityName"
      DataTextField="CityId">
      <asp:ListItem Text="- Select a city -" />
    </asp:DropDownList>
    
    DestinationDropDownList.DataSource = 
      from d in destinations
      select new ListItem() {
        Text = d.CityName,
        Value = d.CityId.ToString()
      };
    DestinationDropDownList.DataBind();