ASP.NET: 如何仅使用文本框创建搜索功能?

ASP.NET: How to create a search function just using a textbox?

我想就在我的网站上添加搜索功能提出建议。将是查找已注册的客户,以便管理员更容易查找特定客户。

能否提供示例代码片段,以便我完成此特定搜索功能?

谢谢。

这是我的 .aspx 代码

  <asp:TextBox ID="txtSearch" runat="server" BorderStyle="Solid" Width="218px" 
        ontextchanged="txtSearch_TextChanged"></asp:TextBox>
&nbsp;<asp:Button ID="btnSearch" runat="server" Text="Search" />
</p>
<div style="overflow-x:auto; width:1200px">
 <asp:GridView ID="gvCustomer" runat="server" AutoGenerateColumns="False" 
        BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" 
        Caption="Customer Profile" CellPadding="4" CellSpacing="2" DataSourceID="SqlDataSourceBM" 
        ForeColor="Black" onrowcommand="gvCustomer_RowCommand" 
        DataKeyNames="ApplicantUsername" >
        <Columns>
            <asp:BoundField DataField="Branch" HeaderText="Branch" 
                SortExpression="Branch" />
            <asp:BoundField DataField="ApplicantUsername" HeaderText="Username" 
                SortExpression="ApplicantUsername" ReadOnly="True" />
            <asp:BoundField DataField="NoAFirstName" HeaderText="First Name" 
                SortExpression="NoAFirstName" />
            <asp:BoundField DataField="NoALastName" HeaderText="Last Name" 
                SortExpression="NoALastName" />
            <asp:ButtonField CommandName="View Profile" HeaderText="Customer Profile" 
                Text="View" />
            <asp:ButtonField CommandName="Edit" HeaderText="Customer Profile" Text="Edit" />
            <asp:ButtonField CommandName="View CR" HeaderText="Credit Report" Text="View" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
        <RowStyle BackColor="White" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#808080" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSourceBM" runat="server" 
        ConnectionString="<%$ ConnectionStrings:PFCIConnectionString %>" 


        SelectCommand="SELECT [ApplicantUsername], [Branch], [NoALastName], [NoAFirstName] FROM [CustomerRegistration] WHERE (([Branch] = @Branch) AND ([NoALastName] LIKE '%' + @NoALastName + '%'))">
        <SelectParameters>
            <asp:SessionParameter Name="Branch" SessionField="ApplicantUsername" 
                Type="String" />
            <asp:ControlParameter ControlID="txtSearch" Name="NoALastName" 
                PropertyName="Text" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

由于您没有在这里提供任何代码并询问方向,所以您开始吧。假设您的 TextBox 名称是“textBox1”并且旁边有一些按钮。在该按钮的单击事件中,您应该在数据库中查询与“textBox1”中的文本匹配的客户名称。您将用于搜索的查询类似于:

SELECT * FROM Customers
WHERE CustomerName LIKE "%" + textBox1.text + "%"; //Assuming the table name is Customers

这应该 return 所有名字中包含 textBox1.text 的客户。希望这会让您对从哪里开始有所了解。干杯,