2011-08-03 140 views
1

我正在嘗試使用名爲search的按鈕使用下拉框執行搜索查詢。我想讓它調出搜索的數據。我不知道從哪裏開始,我已經尋找了一些編碼和不同的方式來做到這一點,但他們似乎很複雜,這是ASP的一點,這是我的弱點,需要一些幫助和指導。下面是該頁面的代碼;需要VB.net幫助搜索查詢?

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="ForSale.aspx.vb" Inherits="Users_ForSale" title="Properties For Sale" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 
    </asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 


    <h3>Properties For Sale</h3> 
    <h5> 
     <asp:Label ID="lblTown" runat="server" Text="Town:"></asp:Label> 
     <asp:DropDownList ID="ddlTownSearch" runat="server"> 
      <asp:ListItem></asp:ListItem> 
      <asp:ListItem>Chadderton</asp:ListItem> 
      <asp:ListItem Value="Failsworth"></asp:ListItem> 
      <asp:ListItem>Oldham</asp:ListItem> 
      <asp:ListItem>Royton</asp:ListItem> 
      <asp:ListItem>Shaw</asp:ListItem> 
     </asp:DropDownList> 
     <asp:Label ID="lblBedroomsSearch" runat="server" Text="Bedrooms:"></asp:Label> 
     <asp:DropDownList ID="DropDownList1" runat="server"> 
      <asp:ListItem></asp:ListItem> 
      <asp:ListItem>1</asp:ListItem> 
      <asp:ListItem Value="2"></asp:ListItem> 
      <asp:ListItem Value="3"></asp:ListItem> 
      <asp:ListItem>4</asp:ListItem> 
      <asp:ListItem>5</asp:ListItem> 
      <asp:ListItem>6+</asp:ListItem> 
     </asp:DropDownList> 
     <asp:Label ID="lblMinPrice" runat="server" Text="Min Price (£):"></asp:Label> 
     <asp:TextBox ID="txtMinPriceSearch" runat="server" Width="87px"></asp:TextBox> 
     <asp:Label ID="lblMaxPriceSearch" runat="server" style="text-align: left" 
      Text="Max Price (£):"></asp:Label> 
     <asp:TextBox ID="TextBox1" runat="server" Width="87px"></asp:TextBox> 
    </h5> 
    <h5> 
     <asp:Button ID="btnForsaleSearch" runat="server" Text="Search" /> 
    </h5> 
    <p> 
     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
      AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
      DataKeyNames="ProductId" DataSourceID="SqlDataSource1" ForeColor="#333333" 
      GridLines="None" Width="588px"> 
      <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> 
      <Columns> 
       <asp:ImageField DataImageUrlField="ImageURL"> 
       </asp:ImageField> 
       <asp:BoundField DataField="ProductId" HeaderText="ProductId" ReadOnly="True" 
        SortExpression="ProductId" /> 
       <asp:BoundField DataField="Description" HeaderText="Description" 
        SortExpression="Description" /> 
       <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" /> 
       <asp:BoundField DataField="Town" HeaderText="Town" 
        SortExpression="Town" /> 
      </Columns> 
      <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> 
      <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
      <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
      <AlternatingRowStyle BackColor="White" /> 
     </asp:GridView> 
    </p> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:JPEstatesConnectionString %>" 
    SelectCommand="SELECT * FROM [Products]" 
     DeleteCommand="DELETE FROM [Products] WHERE [ProductId] = @ProductId" 
     InsertCommand="INSERT INTO [Products] ([ProductId], [Description], [Price], [Category], [ImageURL]) VALUES (@ProductId, @Description, @Price, @Category, @ImageURL)" 
     UpdateCommand="UPDATE [Products] SET [Description] = @Description, [Price] = @Price, [Category] = @Category, [ImageURL] = @ImageURL WHERE [ProductId] = @ProductId"> 
      <DeleteParameters> 
       <asp:Parameter Name="ProductId" Type="String" /> 
      </DeleteParameters> 
      <UpdateParameters> 
       <asp:Parameter Name="Description" Type="String" /> 
       <asp:Parameter Name="Price" Type="Decimal" /> 
       <asp:Parameter Name="Category" Type="String" /> 
       <asp:Parameter Name="ImageURL" Type="String" /> 
       <asp:Parameter Name="ProductId" Type="String" /> 
      </UpdateParameters> 
      <InsertParameters> 
       <asp:Parameter Name="ProductId" Type="String" /> 
       <asp:Parameter Name="Description" Type="String" /> 
       <asp:Parameter Name="Price" Type="Decimal" /> 
       <asp:Parameter Name="Category" Type="String" /> 
       <asp:Parameter Name="ImageURL" Type="String" /> 
      </InsertParameters> 
    </asp:SqlDataSource> 

</asp:Content> 

回答

1

這取決於你的戰略,如果你想用完全一樣的匹配搜索查詢來搜索將是這樣的: SELECT * FROM [Products] WHERE [ProductId][email protected] AND [Price][email protected] AND ...那你需要的其他條件。

,如果你想取回了類似的結果,你要喜歡,而不是使用「=」像這樣的: SELECT * FROM [Products] WHERE [ProductId] LIKE '%' + @ProductId+ '%' AND ([Price] BETWEEN @Price1 AND @Price2) AND ...等條件

價格1和Price2可以是用戶輸入的價格範圍

它可以幫助您使用ASP.NET SQLParameters with LIKE statement

+0

謝謝。這對我更有幫助。再次感謝。 – Naresh

+0

鏈接回答已死 - *「無法到達此網站/無法找到blog.evonet.com.au的服務器DNS地址」* – Pang

+0

@Pang是的,你可以在這裏找到它https://web.archive .ORG /網絡/ 20111010220645/HTTP://blog.evonet.com.au/post/2010/04/24/Using-ParametersAddWithValue-with-a-Like-SQL-query.aspx – Maysam