发出 DropDownList.Items.Addrange 和选择值时出现问题

发布于 2024-09-11 18:38:54 字数 1805 浏览 4 评论 0原文

我是 C# 新手,因为我主要使用 Java。

但直接进入问题(我已经简化了一点):

首先是我的 aspx 页面的摘录:

<div class="dates">
    <div class="firstselection">
        <asp:DropDownList ID="DDFromMonth" runat="server">
        </asp:DropDownList>
        <asp:DropDownList ID="DDToMonth" runat="server">
        </asp:DropDownList>
    </div>
</div>

和代码隐藏:

    public partial class Argh: System.Web.UI.Page
    {
  static  List<ListItem> monthList = new List<ListItem>
  {
   new ListItem("Jan", "1"),
   new ListItem("Feb", "2"),
   new ListItem("Mar", "3"),
   new ListItem("Apr", "4"),
   new ListItem("May", "5"),
   new ListItem("Jun", "6"),
   new ListItem("Jul", "7"),
   new ListItem("Aug", "8"),
   new ListItem("Sep", "9"),
   new ListItem("Oct", "10"),
   new ListItem("Nov", "11"),
   new ListItem("Dec", "12")
  };



        protected void Page_Load(object sender, EventArgs e)
        {
   DDFromMonth.Items.AddRange(monthList.ToArray());  // DDFromMonth is a asp.net dropdown list
   DDToMonth.Items.AddRange(monthList.ToArray()); // DDToMonth is a asp.net dropdown list

            DDFromMonth.SelectedValue = "6"; 
            DDToMonth.SelectedValue = "7"; // also changes DDFromMonth value (GODDAMN!)

 }

好的,我有一个包含月份的静态列表。我将此列表的范围添加到下拉控件和 DDFromMonth 我选择“6”(六月)和 DDToMonth“7”(七月)。然而,这两个下拉列表中的结果都显示七月!

我也尝试过使用非静态列表,但这并不重要。我测试的唯一“有效”的方法是实例化两个不同的列表 - 只有这样才能根据我的意愿选择下拉列表项。因此,添加这样的列表可以使事情正常进行(但很难看):

DDFromMonth.Items.AddRange(monthList.ToArray());
DDFromMonth.Items.AddRange(aDuplicatedMonthList.ToArray()); 

我已经通过 asp.net 添加数据源来解决这个问题,但是,了解这里发生的情况仍然很有趣。为什么我必须为每个下拉列表实例化一个新列表。每个控件不是都有自己的指针来跟踪自己的值吗?感觉好像指针在列表中以某种方式共享,但我将思考留给更了解 C# 和 ASP.NET 的人

I'm new to C#, as I mainly have been using Java.

But straight to the problem (which I have simplified a bit):

First an excerpt of my aspx-page:

<div class="dates">
    <div class="firstselection">
        <asp:DropDownList ID="DDFromMonth" runat="server">
        </asp:DropDownList>
        <asp:DropDownList ID="DDToMonth" runat="server">
        </asp:DropDownList>
    </div>
</div>

And the codebehind:

    public partial class Argh: System.Web.UI.Page
    {
  static  List<ListItem> monthList = new List<ListItem>
  {
   new ListItem("Jan", "1"),
   new ListItem("Feb", "2"),
   new ListItem("Mar", "3"),
   new ListItem("Apr", "4"),
   new ListItem("May", "5"),
   new ListItem("Jun", "6"),
   new ListItem("Jul", "7"),
   new ListItem("Aug", "8"),
   new ListItem("Sep", "9"),
   new ListItem("Oct", "10"),
   new ListItem("Nov", "11"),
   new ListItem("Dec", "12")
  };



        protected void Page_Load(object sender, EventArgs e)
        {
   DDFromMonth.Items.AddRange(monthList.ToArray());  // DDFromMonth is a asp.net dropdown list
   DDToMonth.Items.AddRange(monthList.ToArray()); // DDToMonth is a asp.net dropdown list

            DDFromMonth.SelectedValue = "6"; 
            DDToMonth.SelectedValue = "7"; // also changes DDFromMonth value (GODDAMN!)

 }

Ok, I have a static list that contains the months. I add the range of this list to both dropdown-controls and DDFromMonth I select "6" (June) and DDToMonth "7" (July). However, this results in both dropdownlists show July!

I also tried with a non-static list, but that doesn't matter. The only thing I tested that "works" is to instantiate two different lists - only then the dropdownlist-items are selected as I wish. So, adding the lists like this make things work (but is ugly):

DDFromMonth.Items.AddRange(monthList.ToArray());
DDFromMonth.Items.AddRange(aDuplicatedMonthList.ToArray()); 

I have worked around this by adding datasources via asp.net, but still, it would be interesting to know what is happening here. Why do I have to instantiate a new list for each dropdown list. Doesn't each control have own pointers that keep track of their own values? It feels like the pointers are shared in the list somehow, but I leave ponderings to someone that better knows C# and ASP.NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2024-09-18 18:38:54

ListItem 直接添加到 DropDownList 控件中。因此,实际上您正在做的是将完全相同的列表添加到 2 个不同的控件中。因此,只有最后选择的项目才会被实现,因为同一个列表只能有一个选择的ListItem。

执行您想要的操作的一个简单方法是:

protected ListItem[] MonthList
{
   get
   {
      return new ListItem[]
      {
       new ListItem("Jan", "1"),
       new ListItem("Feb", "2"),
       new ListItem("Mar", "3"),
       new ListItem("Apr", "4"),
       new ListItem("May", "5"),
       new ListItem("Jun", "6"),
       new ListItem("Jul", "7"),
       new ListItem("Aug", "8"),
       new ListItem("Sep", "9"),
       new ListItem("Oct", "10"),
       new ListItem("Nov", "11"),
       new ListItem("Dec", "12")

       };
}

现在您将始终创建一个新数组。

ListItems are added directly to the DropDownList control. So in effect what you are doing is adding the EXACT same list to 2 different controls. Therefore, only the LAST selected item will be implemented, because the same list can only have ONE selected ListItem.

A simple way to do what you want would be this:

protected ListItem[] MonthList
{
   get
   {
      return new ListItem[]
      {
       new ListItem("Jan", "1"),
       new ListItem("Feb", "2"),
       new ListItem("Mar", "3"),
       new ListItem("Apr", "4"),
       new ListItem("May", "5"),
       new ListItem("Jun", "6"),
       new ListItem("Jul", "7"),
       new ListItem("Aug", "8"),
       new ListItem("Sep", "9"),
       new ListItem("Oct", "10"),
       new ListItem("Nov", "11"),
       new ListItem("Dec", "12")

       };
}

Now you'll always create a new array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文