开发者

Search / Filter ASP ListView Using LINQ

开发者 https://www.devze.com 2023-01-12 05:34 出处:网络
I have a ListView on a page that uses a object data source.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\'

I have a ListView on a page that uses a object data source. 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#.

<%@ 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" P开发者_StackOverflow中文版ageSize="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(String filterword)

and provide the text entered into the textbox to the parameter of your ObjectdataSource Select method.

Steps:
1) Change your method in the business class.
2) Rebuild the project and all neccessary libraries
3) Go to the designer and let it refresh the Select Method of your ObjectDataSource. Select as Parametersource None and leave the Defaultvalue blank.
4) Open the EventHandler of the ButtonClick. (Adapt to your actual coding:)

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

精彩评论

暂无评论...
验证码 换一张
取 消