使用下拉列表通过SQL数据源过滤ListView

时间:2019-06-17 13:12:02

标签: c# asp.net

我目前正在一个目前活跃的网站(www.jocogolocal.com)上工作,增强网站所需的一项功能是使用下拉列表而不是使用按钮对供应商列表进行排序专门针对城市和供应商类型的列表,以过滤ListView,以仅显示那些具有活动过滤器的列表。问题是它是一个带有数据源的ListView,每次我尝试调用ListView中的元素时,都会收到一条错误消息,指出“操作员为空”,并指向供应商下方的城市标签。

我尝试使用下拉列表(OnSelectedIndexChange)中的事件来解决我的问题,但是当尝试访问ListView中的属性时,它总是会返回到运算符为null的情况。

<asp:ListView runat="server" ID="livVendor"
    DataKeyNames="VendorID" 
    DataSourceID="sdsListViewVendor" 
    InsertItemPosition="FirstItem"
    >
    <LayoutTemplate>
        <asp:Table runat="server" Font-Names="Century Gothic" Font-Size="12px">
            <asp:TableRow>
                <asp:TableCell>
                    <table id="itemPlaceholderContainer">
                        <tr>
                            <td>
                                <asp:Label ID="lblCityTest" runat="server" Font-Bold="true" Text="City" />&nbsp
                                <asp:DropDownList runat="server" ID="ddlCity" AutoPostBack="true" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged">
                                    <asp:ListItem Value="Bargersville">Bargersville</asp:ListItem>
                                    <asp:ListItem Value="Carthage">Carthage</asp:ListItem>
                                    <asp:ListItem Value="Edinburgh">Edinburgh</asp:ListItem>
                                    <asp:ListItem Value="Franklin">Franklin</asp:ListItem>
                                    <asp:ListItem Value="Greenfield">Greenfield</asp:ListItem>
                                    <asp:ListItem Value="Greenwood">Greenwood</asp:ListItem>
                                    <asp:ListItem Value="Indianapolis">Indianapolis</asp:ListItem>
                                    <asp:ListItem Value="Roanoke">Roanoke</asp:ListItem>
                                    <asp:ListItem Value="Trafalgar">Trafalgar</asp:ListItem>
                                    <asp:ListItem Value="Whiteland">Whiteland</asp:ListItem>
                                </asp:DropDownList>&nbsp
                                <asp:Label runat="server" Font-Bold="true" Text="Vendor Type" />&nbsp
                                <asp:DropDownList runat="server" ID="ddlVendorType" DataSourceID="sdsDropDownListVendorType" DataTextField="VendorType" DataValueField="VendorTypeID" AutoPostBack="true" />&nbsp
                                <asp:Button runat="server" CommandArgument="Name" CommandName="Sort"  />&nbsp
                                <asp:Button runat="server" CommandArgument="City" CommandName="Sort" Text="City" />&nbsp
                                <asp:Button runat="server" CommandArgument="VendorType" CommandName="Sort" Text="Vendor Type" />&nbsp
                            </td>
                        </tr>
                        <tr>
                            <td><asp:Label runat="server" Text="<br /> <br />"/></td>
                        </tr>
                        <tr runat="server" id="itemPlaceholder" />
                    </table>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </LayoutTemplate>
    <ItemTemplate>
            <tr>
            <td>
            <div class="container">
                <div class="row">
                  <div class="col-xs-auto">
                <p><asp:Label runat="server" ID="lblName" Enabled="false" Font-Bold="true" Font-Size="X-Large" Text='<%# Bind("Name") %>'/></p>
                </div>
                </div>
            </td>
            </tr>
            <tr>
            <td>
                <asp:Label runat="server"  ID="lblVendorType" Enabled="false" Text='<%# Bind("VendorType") %>' />
            </td>
            </tr>
            <tr>
            <td>
                <asp:Label runat="server" ID="lblAddress" Enabled="false" Text='<%# Bind("Address") %>'/>
            </td>
            </tr>
            <tr>
            <td>
                <asp:Label runat="server" ID="lblCity" Enabled="false" Text='<%# Bind("City") %>'/>
                <asp:Label runat="server" ID="lblState" Enabled="false"  Text='<%# Bind("State") %>' />
                <asp:Label runat="server" ID="lblZipCode" Enabled="false" Text='<%# Bind("ZipCode") %>'/>
            </td>
            </tr>
            <tr>
            <td>
                <asp:Label runat="server" ID="lblPhoneNumber" Enabled="false" Text='<%# Bind("PhoneNumber") %>'/>
            </td>
            </tr>
            <tr>
            <td>
                <asp:HyperLink runat="server" ID="hypWebsite" Target="_blank" NavigateUrl='<%# Bind("Website") %>' Text='<%# Bind("Website") %>' />
            </td>
            </tr>
            <tr>
            <td>
                <asp:Label runat="server" ID="lblDescription" Enabled="false" Text='<%# Bind("Description") %>' />
            </td>
            </tr>
            <tr>
            <td>
                <asp:HyperLink runat="server" ID="hypLocate" Text="Locate" CssClass="button" Target="_blank" NavigateUrl='<%# Eval("Name", "https://google.com/maps/search/ {0}") + " " + Eval("Address") + " " + Eval("City") + " " + Eval("State") + " " + Eval("Zipcode") %>' />

                </div>
                <br /><br /><br />
            </td>
            </tr>
    </ItemTemplate>
    <InsertItemTemplate>
    </InsertItemTemplate>
</asp:ListView>
<br /><br />

我希望DropDownLists仅显示具有与DropDownList过滤器上所选属性相同的属性的供应商。任何帮助将不胜感激

编辑:

这是ViewVendor.cs文件中我要至少测试的内容:

    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindData((livVendor.FindControl("ddlCity") as DropDownList).SelectedValue);
    }

    protected void bindData(string userChoice)
    {
        Label lblCity = livVendor.FindControl("lblCity") as Label;
        DropDownList ddlCity = livVendor.FindControl("ddlCity") as DropDownList;
        ddlCity.DataSource = sdsDropDownListCity;
        lblCity.DataBind();
        lblCity.Text = ddlCity.SelectedValue;


    }
}

}

在这里,我正在检查是否至少可以访问lblCity来将其更改为选定的值,但是每次执行ddlCity_SelectedValue时,都会收到错误消息:System.NullReferenceException:'对象引用未设置为的实例对象。”,因此,如果我无法使这部分工作,那么我可以过滤列表视图以仅显示具有所选值的供应商的机会是什么?

1 个答案:

答案 0 :(得分:0)

结果我真正需要做的就是操作.cs文件中的DataSource.SelectCommand,例如:

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlCity = livVendor.FindControl("ddlCity") as DropDownList;
        sdsListViewVendor.SelectCommand = "SELECT * FROM Vendor, VendorType WHERE VendorType.VendorTypeID = Vendor.VendorTypeID AND Vendor.City = '" + ddlCity.SelectedValue + "' ORDER BY Name";

这也可以用于其他下拉列表来操作ListView。感谢用户刘镇玱和其他人的贡献。