2016-01-21 45 views
-11

我想在我的網站上提供有關搜索功能的建議。這將是找到註冊的客戶,以便管理員更容易尋找特定的客戶。ASP.NET:如何使用文本框創建搜索功能?

你可以提供示例代碼片段,以便我可以完成此特定搜索功能嗎?

謝謝。

這裏是我的.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> 
+3

我想你將不得不爲自己付出更多的努力,並在你得到任何答案之前在你的問題中包括你所嘗試過的東西。就目前而言,這個問題是廣泛的(也可能是基於意見的) –

+1

看起來有些人正在從快餐連鎖店訂購一些代碼。 – Aizen

+0

真的嗎? <3多好<3 – Edge

回答

0

既然你在這裏沒有提供任何代碼,並要求對方向,所以在這裏你去。讓我們假設你的TextBox名字是'textBox1',旁邊有一些按鈕。在該按鈕的點擊事件中,您應該查詢數據庫中與「textBox1」內的文本相匹配的客戶名稱。你會使用您的搜索查詢會是這樣的:

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

這將返回所有誰擁有textBox1.text他們的名字裏面的客戶。希望這會給你一些洞察從哪裏開始。歡呼聲,

+0

如果他們沒有提供代碼,請不要回答。你不必回答每一個問題,事實上,你應該*避免*回答不好的問題。 – mason

+0

嚇了一大跳,我提供了代碼。大聲笑 – Edge