简体   繁体   中英

How to search data in asp.net web application

I need help with my asp.net web app. I have a page where i display data (all the users) with datagrid. For now, there is no problem when i only have a few users in. But when a database will be populated with 100+ users, it will be difficult to find specified users.

I need to create a search engine (search by name). I already found a few tutorials, but they are all based on creating a new datagrid and creating a new sql data source.

I need to make sure that when admin types in data to find members, it display the searched user in the same datagrid. I already have a code for delete and edit data, can somebody please help me create a code or at least guide me, how to make a search engine? Thank you

Here is my code:

<asp:CommandField CancelText="Cancel" DeleteText="Delete" EditText="Edit" ShowDeleteButton="true" SelectText="Select" UpdateText="Update" ShowEditButton="True" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Users] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Users] ([Id], [Name], [LastName],) VALUES (@Id, @Name, @LastName,)" SelectCommand="SELECT * FROM [Users]" UpdateCommand="UPDATE [Users] SET [Name] = @Name, [LastName] = @LastName, WHERE [Id] = @Id">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Id" Type="Int32" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

EDIT:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource">
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="true" SortExpression="Id" Visible="false" />
            <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" >                    
                <ItemStyle Width="150px" />
            </asp:BoundField>
            <asp:BoundField DataField="lastName" HeaderText="LastName" SortExpression="lastName" >
                <ControlStyle Width="100px" />
                <ItemStyle Width="150px" />
            </asp:BoundField>

As I understand you need to filter by Name the Grid Results. First of all Change your Select query to add a Filter.

SELECT * FROM [Users] where ((not @Name is null and [Name] like '%' + @Name + '%'))

Then add a Control parameter to your SqlDatasource:

<SelectParameters>
<asp:ControlParameter ControlID="TextboxName" Name="Name" PropertyName="Text" ConvertEmptyStringToNull="False" />
</SelectParameters>

Then add to your search button this code behind:

  protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }

Hope it helps you.

Use Filter Expression

Modify your SqlDatasource as

 <asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Users] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Users] ([Id], [Name], [LastName],) VALUES (@Id, @Name, @LastName,)" SelectCommand="SELECT * FROM [Users]" FilterExpression="Name LIKE '%{0}%' UpdateCommand="UPDATE [Users] SET [Name] = @Name, [LastName] = @LastName, WHERE [Id] = @Id">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Id" Type="Int32" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
        </UpdateParameters>
<FilterParameters>
                <asp:ControlParameter ControlID="txtSearch" PropertyName="Text" DefaultValue="%" />
            </FilterParameters>
    </asp:SqlDataSource>

Add

FilterExpression="Name LIKE '%{0}%'

AND

<FilterParameters>
   <asp:ControlParameter ControlID="txtSearch" PropertyName="Text" DefaultValue="%" />
</FilterParameters>

Please replace that the txtSearch I used for ControlID with the ID of your textbox which is used for search.

Use backgrid

for avoiding multiple datasources it performs client side filtering and vary fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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