如何搜索GridView?

时间:2015-11-25 09:09:38

标签: c# asp.net gridview search-box

我有一个文本框和搜索按钮。目前,“搜索”按钮不执行任何操作。我不知道如何在GridView中搜索。我已经研究了几个小时并且示例不起作用,因为我使用存储过程(一个是适用于此GridView的select语句,同时我看到我需要一些其他选择来获取搜索...但我不知道看看如何在这里加入两个选项)。如何在此处添加搜索?

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

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h1>View Registrant</h1>

    <asp:TextBox ID="TxtSearch" runat="server" CssClass="formtextbox"></asp:TextBox>
        <asp:Button ID="BtnSearch" runat="server" onclick="BtnSearch_Click" Text="Search" CssClass="formbutton" />


        <asp:GridView ID="GridView1"  runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSourceViewRegistrant"  
            CssClass="mGrid"  
    PagerStyle-CssClass="pgr"  
    AlternatingRowStyle-CssClass="alt" >
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>

            <Columns>


                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />


                <asp:BoundField DataField="LocalNumberId" HeaderText="Local" 
                    SortExpression="LocalNumberId" />
                <asp:BoundField DataField="LocalUnionEmail" HeaderText="Local Email" 
                    SortExpression="LocalUnionEmail" />
                <asp:BoundField DataField="DistrictId" HeaderText="District" 
                    SortExpression="DistrictId" />
                <asp:BoundField DataField="CompletedByFirstName" HeaderText="Completed By First Name" 
                    SortExpression="CompletedByFirstName" />
                <asp:BoundField DataField="CompletedByLastName" HeaderText="Completed By Last Name" 
                    SortExpression="CompletedByLastName" />
                <asp:BoundField DataField="CompletedByPhone" HeaderText="Completed By Phone" 
                    SortExpression="CompletedByPhone" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" 
                    SortExpression="LastName" />
                <asp:BoundField DataField="Position" HeaderText="Position" 
                    SortExpression="Position" />
                <asp:BoundField DataField="Email" HeaderText="Email" 
                    SortExpression="Email" />
                <asp:BoundField DataField="ShirtSize" HeaderText="Shirt Size" 
                    SortExpression="ShirtSize" />
                <asp:BoundField DataField="Comments" HeaderText="Comments" 
                    SortExpression="Comments" />
            </Columns>

<PagerStyle CssClass="pgr"></PagerStyle>

        </asp:GridView>
    <div style=" width:100%; overflow: hidden;">
        <asp:SqlDataSource ID="SqlDataSourceViewRegistrant" runat="server" 
            ConnectionString="<%$ ConnectionStrings:Events2 %>" 
            DeleteCommand="spDelRegistrant" DeleteCommandType="StoredProcedure" 
            InsertCommand="spInsRegistrantPaper" InsertCommandType="StoredProcedure" 
            SelectCommand="spGetRegistrantPaper" SelectCommandType="StoredProcedure" 
            UpdateCommand="spUpdateRegistrantPaper" 
            UpdateCommandType="StoredProcedure">
            <DeleteParameters>
                <asp:Parameter Name="RegistrantId" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="EventId" Type="Int32" />
                <asp:Parameter Name="LocalNumber" Type="Int32" />
                <asp:Parameter Name="LocalUnionEmail" Type="String" />
                <asp:Parameter Name="DistrictId" Type="Int32" />
                <asp:Parameter Name="CompletedByFirstName" Type="String" />
                <asp:Parameter Name="CompletedByLastName" Type="String" />
                <asp:Parameter Name="CompletedByPhone" Type="String" />
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="Position" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
                <asp:Parameter Name="ShirtSize" Type="String" />
                <asp:Parameter Name="Comments" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="RegistrantId" Type="Int32" />
                <asp:Parameter Name="LocalNumber" Type="Int32" />
                <asp:Parameter Name="LocalUnionEmail" Type="String" />
                <asp:Parameter Name="DistrictId" Type="Int32" />
                <asp:Parameter Name="CompletedByFirstName" Type="String" />
                <asp:Parameter Name="CompletedByLastName" Type="String" />
                <asp:Parameter Name="CompletedByPhone" Type="String" />
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="Position" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
                <asp:Parameter Name="ShirtSize" Type="String" />
                <asp:Parameter Name="Comments" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        </div>
    </p>

</asp:Content>

更新

 <SelectParameters>
                <asp:ControlParameter ControlID="TxtSearch" Name="FirstName" PropertyName="Text" Type="String" DefaultValue="%%" />
                <asp:Parameter Name="FirstName" Type="String" />
            </SelectParameters>

存储过程:

USE [Events2]
GO
/****** Object:  StoredProcedure [dbo].[spGetRegistrantPaper]    Script Date: 11/25/2015 11:10:14 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spGetRegistrantPaper]
    -- Add the parameters for the stored procedure here
    @FirstName nVarChar (255)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT LocalNumberId,
        LocalUnionEmail,
        DistrictId,
        CompletedByFirstName,
        CompletedByLastName,
        CompletedByPhone,
        FirstName,
        LastName,
        Position,
        Email,
        ShirtSize,
        Comments
        FROM Registrant
        WHERE FirstName LIKE '%' + @FirstName + '%'

END

1 个答案:

答案 0 :(得分:2)

您必须修改存储过程spGetRegistrantPaper才能将过滤后的记录发送到Web应用程序。您可以使用Where这样的子句(例如)修改它: -

SELECT * FROM RegistrantPaper WHERE FirstName LIKE '%' + @Name + '%'

其中,@Name将是您SP中的输入参数。

接下来,为您的SQLDataSource标记添加选择参数,如下所示: -

<SelectParameters>
    <asp:ControlParameter ControlID="TxtSearch" Name="Name" PropertyName="Text" 
         Type="String" DefaultValue="%%" />
</SelectParameters>