简体   繁体   English

使用LINQ搜索/筛选ASP ListView

[英]Search / Filter ASP ListView Using LINQ

I have a ListView on a page that uses a object data source. 我在使用对象数据源的页面上有一个ListView。 I want to be able to add a search box to the page and only show the results that match the search query. 我希望能够在页面上添加搜索框,并且仅显示与搜索查询匹配的结果。 Does anyone have a good reference? 有人参考吗? I'm using C#. 我正在使用C#。

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="system.aspx.cs" Inherits="system" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:Label ID="LabelSearch" runat="server" Text="Search: " />
    <asp:TextBox ID="TextSearchBox" runat="server" />
    <asp:ImageButton ID="ButtonSearchBox" runat="server" ImageUrl="~/Styles/Images/Find.png"
        OnClick="ButtonSearchBox_Click" />&nbsp;
    <asp:Label ID="LabelSystemCount" runat="server" />
    <asp:ListView ID="SystemList" runat="server" DataSourceID="SystemSource" DataKeyNames="SystemID">
        <ItemTemplate>
            <tr id="row" runat="server" class='<%# Container.DataItemIndex % 2 == 0 ? "row" : "altrow" %>'>
                <td>
                    <%# Eval("Name") %>
                </td>
                <td>
                    <%# Eval("Acronym") %>
                </td>
                <td>
                    <%# Eval("Description") %>
                </td>
                <td>
                    <asp:ImageButton ID="ButtonEdit" runat="server" ImageUrl="~/Styles/Images/Edit.png"
                        ToolTip="Edit" OnClick="ButtonEdit_Click" />
                    <asp:ImageButton ID="ButtonDelete" runat="server" ImageUrl="~/Styles/Images/Delete-Red-Cross.png"
                        ToolTip="Delete" CommandName="Delete" />
                </td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table class="system">
                <tr>
                    <th>
                        <asp:LinkButton runat="server" Text="System Name" />
                    </th>
                    <th>
                        <asp:LinkButton runat="server" Text="Acronym" />
                    </th>
                    <th>
                        <asp:LinkButton runat="server" Text="Description" />
                    </th>
                    <th>
                        <asp:Label runat="server" Text="" />
                    </th>
                </tr>
                <tr id="itemPlaceholder" runat="server" />
            </table>
        </LayoutTemplate>
    </asp:ListView>
    <asp:DataPager ID="SystemPager" runat="server" PageSize="10" PagedControlID="SystemList">
        <Fields>
            <asp:NumericPagerField />
        </Fields>
    </asp:DataPager>
    <asp:ObjectDataSource ID="SystemSource" runat="server" DataObjectTypeName="cipfinModel.System"
        DeleteMethod="Delete" SelectMethod="GetSystems" SelectCountMethod="SystemCount"
        TypeName="SystemDAO" InsertMethod="Insert" UpdateMethod="Update" EnablePaging="true" />
</asp:Content>

Change your GetSystems method to 将您的GetSystems方法更改为

GetSystems(String filterword)

and provide the text entered into the textbox to the parameter of your ObjectdataSource Select method. 并将在文本框中输入的文本提供给ObjectdataSource Select方法的参数。

Steps : 步骤
1) Change your method in the business class. 1)在商务舱中更改您的方法。
2) Rebuild the project and all neccessary libraries 2)重建项目和所有必要的库
3) Go to the designer and let it refresh the Select Method of your ObjectDataSource. 3)转到设计器,让其刷新ObjectDataSource的Select方法。 Select as Parametersource None and leave the Defaultvalue blank. 选择作为参数源无,并将“默认值”留空。
4) Open the EventHandler of the ButtonClick. 4)打开ButtonClick的EventHandler。 (Adapt to your actual coding:) (适应您的实际编码:)

this.SystemSource.SelectParameters["PARAMETERNAME"].DefaultValue = TextSearchBox.Text;
this.SystemSource.Databind();
this.SystemList.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM