筛选器下拉列表选项服务器端asp.net

时间:2019-04-08 21:52:15

标签: asp.net vb.net

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

问题和请求

我在更新面板中有两个下拉列表控制器,每当用户在“部分页面回发”中的国家/地区下拉列表中更改国家/地区时,我都试图重新填充状态下拉列表。在国家/地区下拉列表中更改国家/地区时,我目前遇到错误:

数据绑定:“ System.Data.DataRow”不包含名称为“名称”的属性。

我不确定为什么会给我这个错误,但是所有输入都是有帮助的。我有一个解决方案,可以给我想要的结果,但是可以在JS和客户端进行。我想改为处理此服务器端。

下面是国家和州/省/自治区/直辖市/自治区表的简短摘录,以及我当前的带有代码后置代码的代码。

我还做了一个快速功能,可以创建状态DataTable。(如果有帮助的话)

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    |
-------------------------

1 个答案:

答案 0 :(得分:0)

我考虑了我的问题,并开始遍历我可用的方法,然后看到select t.customerid from t left join t t1 on t1.attributeid = t.attributeid and t1.customerid = 'Customer1' group by t.customerid having count(t.customerid) = count(t1.customerid); ,它是DataTableExtensions类的方法。所以我改变了:

原始

CopyToDataTable

收件人

已更新

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

这给了我想要的结果。