【问题标题】:Why might dropdownlist.SelectedIndex = value fail?为什么 dropdownlist.SelectedIndex = value 可能会失败?
【发布时间】:2009-11-13 14:55:42
【问题描述】:

我有一个绑定到数据表的下拉列表。这是我用来执行此操作的代码:

ddlBuildAddr.DataSource = buildings
ddlBuildAddr.DataTextField = "buildingName"
ddlBuildAddr.DataValueField = "buildingId"
Dim addressId As Int32 = OfficeData.GetInstance().GetBuildingId(currentAddress)
ddlBuildAddr.SelectedIndex = addressId
ddlBuildAddr.DataBind()

不幸的是,ddlBuildAddr.SelectedIndex = addressId 行失败了。通过调试器查看这一行,SelectedIndex 变为 -1,而addressId 变为 2。什么给出了?为什么赋值运算符 flatout 不起作用?

【问题讨论】:

    标签: asp.net vb.net drop-down-menu


    【解决方案1】:

    在您尝试设置所选索引之前,将您的 ddlDeptName.DataBind() 移动到。在绑定之前,下拉列表中实际上没有任何项目,因此索引 2 是无效的。

    【讨论】:

    • 愚蠢的错误,你是对的!但是,selectedindex 仍然没有改变。
    • 您是否验证过下拉菜单实际上已填满了任何内容?尝试注释掉该行以设置索引并验证是否有项目被加载到列表中。此外,正如其他人所建议的那样,您可能想要设置 SelectedValue 属性。
    • Argh - 错过了 DataBind() 调用。好收获!
    【解决方案2】:

    替换此行

    ddlDeptName.SelectedIndex = addressId
    

    有了这个:

    ddlDeptName.SelectedValue = addressId.ToString()
    

    至于失败的原因 - addressId 可能超出了您的下拉列表的可能 index 值范围。

    【讨论】:

    • 如果您使用 ToString,那么您会想出与我建议的完全相同的东西,但您说它不起作用……嗯?
    【解决方案3】:

    我认为您需要设置SelectedValue 属性。

    【讨论】:

    • SelectedValue 返回一个空字符串。
    【解决方案4】:

    正如 TLiebe 所指出的,在您尝试设置所选内容之前,将您的数据绑定移动到。数据绑定方法基本上会清除您在组合框上设置的任何先前状态。

    其次,所选索引不是数据值或所选值成员。它是下拉列表项目集合中 ListItem 的索引,这样 dropdown[ dropdown.selectedindex] 将为您提供列表中标记为选中的项目。因此,您必须找到要选择的项目,然后将 selectedindex 设置为该项目的索引。

    来自MSDN的VB示例:

    ' Selects the item whose text is Apples
    ListBox1.Items.FindByText("Apples")
    If Not li Is Nothing Then
       li.Selected = True
    End If
    
    // Selects the item whose text is Apples
    ListItem li = ListBox1.Items.FindByText("Apples");
    if(li != null)
    {
       li.Selected = true;
    }
    

    【讨论】:

      【解决方案5】:

      使用这个

      ddlBuildAddr.DataSource = buildings
      ddlBuildAddr.DataTextField = "buildingName"
      ddlBuildAddr.DataValueField = "buildingId"
      Dim addressId As Int32 = OfficeData.GetInstance().GetBuildingId(currentAddress)
      
      ddlBuildAddr.Databind()
      

      之后

      foreach (var item in ddlBuildAddr.Items)
      {
        if(Convert.toInt32(item.value)==addressId)
        {
           item.selected=true;
           break;
        }
      }
      

      【讨论】:

        【解决方案6】:

        如下修改你的代码....

        ddlBuildAddr.DataSource = buildings
        ddlBuildAddr.DataTextField = "buildingName"
        ddlBuildAddr.DataValueField = "buildingId"
        ddlBuildAddr.DataBind()
        
        Dim addressId As Int32 = OfficeData.GetInstance().GetBuildingId(currentAddress)
        
        ddlBuildAddr.SelectedValue = addressId //It may throw error if item is not found in the list
        (or)
        ddlBuildAddr.Items.FindByValue(addressId).Selected = true;
        (or)
        ListItem lstNew = ddlBuildAddr.Items.FindByValue(addressId)
        ddlBuildAddr.selectedItem = lstNew
        

        【讨论】:

          猜你喜欢
          • 2012-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-11
          • 2021-10-10
          • 2013-01-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多