【问题标题】:Filter dropdown list options server-side asp.net过滤下拉列表选项服务器端 asp.net
【发布时间】:2019-08-30 03:00:10
【问题描述】:

我的数据库中有两个“大”表。这两个表包含大量的州和国家/地区。

问题和请求

我在更新面板中有两个下拉列表控制器,每次用户在部分页面 PostBack 的国家下拉列表中更改国家/地区时,我都会尝试重新填充状态下拉列表。我目前在国家/地区下拉列表中更改国家/地区时出现错误:

DataBinding:“System.Data.DataRow”不包含名为“Name”的属性。

我不知道为什么它给了我这个错误,但所有的输入都是有帮助的。我有一个解决方案可以给我想要的结果,但它是在 JS 和客户端完成的。我想改为处理这个服务器端。

以下是国家和州表的简短摘录以及我当前的代码隐藏代码。

我还做了一个快速函数来创建状态数据表。(如果这有帮助)

Form.ascx

<asp:UpdatePanel runat="server" ID="pnlAddressSelect" UpdateMode="Conditional">
  <ContentTemplate>
    <!-- State -->
    <div class="fieldGroup">
      <p class="inputLabel b f4"><span class="required-label"></span>State/Province:</p>
      <asp:DropDownList runat="server" ID="ddlState" AppendDataBoundItems="true" CssClass="inputField" autocomplete="new-state" />
    </div>

    <!-- Country -->
    <div class="fieldGroup">
      <p class="inputLabel b f4"><span class="required-label"></span>Country:</p>
      <asp:DropDownList runat="server" ID="ddlCountry" AppendDataBoundItems="true" CssClass="inputField" autocomplete="new-country" OnSelectedIndexChanged="DdlCountry_SelectedValueChanged" />
    </div>
  </ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
  </Triggers>
</asp:UpdatePanel>

Form.ascx.vb(代码隐藏)

Public Function GetTable() As DataTable
  Dim table As New DataTable
      With table
          With.Columns
            .Add("value", GetType(String))
            .Add("Name", GetType(String))
          End With
          With.Rows
            .Add("220-220", "Kaliro")
            .Add("220-221", "Manafwa")
            .Add("220-222", "Namutumba")
        .Add("22-05", "Vidin")
        .Add("22-06", "Vratsa")
            .Add("22-07", "Gabrovo")    
        .Add("222-AK", "Alaska")
            .Add("222-AL", "Alabama")
          .Add("222-AR", "Arkansas")
        .Add("223-AR", "Artigas")
        .Add("223-CA", "Canelones")
        .Add("223-DU", "Durazno")
      End With
    End With
  Return table
End Function

Public Property CountryCode As String = "-1"
ReadOnly DefaultOption As ListItem = New ListItem("--Select--", "-1")

Protected Sub DdlCountry_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs)
    'Save the selected country code to the public property
    CountryCode = ddlCountry.SelectedValue       

    'Populate States
    'Dim StatesSQL As String = "..spGetAllStatesForSearch",
    '  StatesTable As DataTable = DataAction.GetDataTable(StatesSQL)
    ' The above will pull the DataTable from the Database but for this
    ' example. I provided a function that creates the table specifically
    ' for the states. The country is not needed in this as this value can
    ' be manually set for this example.

    Dim StatesTable As DataTable = GetTable()


    Dim FilteredStates As List(Of DataRow) = (From state In StatesTable.AsEnumerable Select state Where state.Field(Of String)("value").StartsWith(CountryCode & "-")).ToList

    'Clear the options from the dropdown list first before binding
    ddlState.Items.Clear()
    ddlState.DataSource = FilteredStates
    ddlState.Items.Add(DefaultOption)
    ddlState.DataTextField = "Name"
    ddlState.DataValueField = "value"
    ddlState.DataBind()

    ' The database has the first row as an empty value,
    ' So I replace this with the custom option 'DefaultOption' that I defined above
    ddlState.Items.RemoveAt(1)


End Sub

出于本示例的目的,我将限制下表中的条目。因为它们组合了超过 3K 条目,除了表结构之外并没有真正为问题增加太多价值。

国家/地区表

| (String) | (String)      |
| id       | country       |
----------------------------
| 222      | United States |
| 22       | Bulgaria      |
| 220      | Uganda        |
| 223      | Uruguay       |
----------------------------

状态表

| (String)  | (String)   |
| value     | Name       |
--------------------------
| 220-220   | Kaliro     |
| 220-221   | Manafwa    |
| 220-222   | Namutumba  |
| 22-05     | Vidin      |
| 22-06     | Vratsa     |
| 22-07     | Gabrovo    |
| 222-AK    | Alaska     |
| 222-AL    | Alabama    |
| 222-AR    | Arkansas   |
| 223-AR    | Artigas    |
| 223-CA    | Canelones  |
| 223-DU    | Durazno    |
-------------------------

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    我考虑了我的问题并开始运行我可用的方法,我看到CopyToDataTable 这是 DataTableExtensions 类的一个方法。所以我改变了:

    原创

    Dim FilteredStates As List(Of DataRow) = (From state In StatesTable.AsEnumerable Select state Where state.Field(Of String)("value").StartsWith(CountryCode & "-")).ToList  
    

    更新

    Dim FilteredStates As DataTable = (From state In StatesTable.AsEnumerable Select state Where state.Field(Of String)("value").StartsWith(CountryCode & "-")).CopyToDataTable  
    

    这给了我想要的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      • 2020-10-18
      • 1970-01-01
      相关资源
      最近更新 更多