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

时间:2016-01-21 02:13:39

标签: c# asp.net sql-server database 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>

1 个答案:

答案 0 :(得分:1)

由于您未在此处提供任何代码并询问方向,因此请转到此处。假设您的TextBox名称为“textBox1”,旁边有一个按钮。在该按钮的单击事件中,您应该在数据库中查询与“textBox1”内的文本匹配的客户名称。您将用于搜索的查询类似于:

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

这应该返回名称中包含textBox1.text的所有客户。希望这能让您了解从哪里开始。干杯,,