简体   繁体   English

如何在asp.net Web应用程序中搜索数据

[英]How to search data in asp.net web application

I need help with my asp.net web app. 我的asp.net网络应用程序需要帮助。 I have a page where i display data (all the users) with datagrid. 我有一个页面,我用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. 现在,当我只有少数用户时,没有问题。但是当一个数据库将填充100多个用户时,很难找到指定的用户。

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. 我已经找到了一些教程,但它们都基于创建新的数据网格并创建新的SQL数据源。

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查询以添加过滤器。

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

Then add a Control parameter to your SqlDatasource: 然后将一个Control参数添加到您的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 将您的SqlDatasource修改为

 <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. 请将我用于ControlID的txtSearch替换为用于搜索的文本框的ID。

Use backgrid 使用backgrid

for avoiding multiple datasources it performs client side filtering and vary fast. 为避免多个数据源,它执行客户端过滤并快速变化。

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

相关问题 如何隐藏搜索机器人的Web应用程序? (ASP.NET) - How to hide web-application from search bots? (ASP.NET) 如何将数据从Windows窗体应用程序发送到Asp.Net Web服务应用程序 - How to send data from a Windows Form Application to a Asp.Net Web Service Application 如何将文件从Web下载到ASP.NET Web应用程序中? - How to download files from web into an ASP.NET Web Application? 将Google Analytics(分析)数据提取到Asp.net Web应用程序中 - Pulling google analytics data into Asp.net web application 将数据从表中获取到ASP.NET Web应用程序 - Getting data from a table to ASP.NET web application ASP.net Web应用程序数据完整性问题 - ASP.net web application data integrity issue asp.net web 应用后台隐藏数据字段 - asp.net web application hide data field at the backend 从ASP.Net 4.5向ASP.Net 2.0 Web应用程序发布数据时出错 - Error while posting data from ASP.Net 4.5 to ASP.Net 2.0 web application ASP.NET中如何实现搜索过滤 Core Web API - How to implement search filter in ASP.NET Core Web API 如何将参数传递给在ASP.NET Web应用程序中具有对象数据源的本地报表? - How to pass parameters to a Local report that has object data source in asp.net web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM