如何根据asp.net中的下拉列表在gridview上显示结果?

时间:2013-05-29 21:28:13

标签: asp.net database gridview

我正在制作搜索页面并显示结果。这是UI:

enter image description here

我在gridview上显示结果。现在,我只按标题搜索,但我在下拉列表中有其他选项。我想根据下拉列表的选定值编写查询,然后在gridview中显示它们。我怎样才能做到这一点?以下是搜索页面的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Pages_Search" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: right">

    <asp:LinkButton ID="logout" runat="server" onclick="logout_Click">Log out</asp:LinkButton>

</div>
<br />
<br />
Search For Books:&nbsp;
<asp:TextBox ID="tSearchBox" 
             Text="Catalog Search" 
        onfocus="if(this.value=='Catalog Search')this.value='';"     onblur="if(this.value=='')this.value='Catalog Search';"
                 runat="server" Height="17px" Width="189px" ForeColor="#CCCCCC" 
        >Catalog Search</asp:TextBox>
&nbsp;&nbsp;&nbsp;
<asp:Button ID="bSearchButton" runat="server" Height="24px" 
    onclick="bSearchButton_Click" PostBackUrl="SearchResults.aspx" Text="Search" 
    Width="119px" />
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:DropDownList ID="SearchBy" runat="server" Height="17px" Width="188px" 
    onselectedindexchanged="SearchBy_SelectedIndexChanged">

 <asp:ListItem>Search by title</asp:ListItem>
    <asp:ListItem>Search by author</asp:ListItem>
    <asp:ListItem>Search by item type</asp:ListItem>
    <asp:ListItem>Search by publish year</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<br />
<br />
    <asp:LinkButton ID="lReturnHome" runat="server" onclick="lReturnHome_Click">Return Homepage</asp:LinkButton>
<br />
</form>
</body>
</html>

以下是我显示结果的页面的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchResults.aspx.cs"  Inherits="Pages_SearchResults" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: right">

    <asp:LinkButton ID="logout" runat="server" onclick="logout_Click">Log out</asp:LinkButton>

</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ISBN" DataSourceID="SqlDataSource1" 
    onselectedindexchanged="GridView1_SelectedIndexChanged" 
    onrowcommand="GridView1_RowCommand">
    <Columns>
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="ISBN" HeaderText="ISBN" ReadOnly="True" 
            SortExpression="ISBN" />
        <asp:BoundField DataField="AuthorName" HeaderText="AuthorName" 
            SortExpression="AuthorName" />
        <asp:BoundField DataField="AuthorlName" HeaderText="AuthorlName" 
            SortExpression="AuthorlName" />
        <asp:BoundField DataField="ItemType" HeaderText="ItemType" 
            SortExpression="ItemType" />
        <asp:BoundField DataField="PublishYear" HeaderText="PublishYear" 
            SortExpression="PublishYear" />



    </Columns>
 </asp:GridView>
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Items] WHERE ([Title] LIKE '%' + @Title + '%')">
    <SelectParameters>
          <asp:FormParameter FormField="tSearchBox" Name="Title" Type="String" />
     </SelectParameters>
 </asp:SqlDataSource>
 <br />
     <asp:Label ID="Label1" runat="server" Text="Type ISBN to loan:"></asp:Label>
&nbsp;&nbsp;
<asp:TextBox ID="tLoanBox" runat="server" Width="137px"></asp:TextBox>
&nbsp;&nbsp;&nbsp;
<asp:Button ID="bLoanButton" runat="server" onclick="bLoanButton_Click" 
    PostBackUrl="~/Pages/SearchResults.aspx" Text="Loan" Width="103px" />
&nbsp;&nbsp;&nbsp;
<asp:Label ID="loanSuccesful" runat="server" 
    style="color: #00CC00; font-weight: 700" Text="Loan Succesful" Visible="False">    </asp:Label>
<br />
<br />
<asp:Label ID="notAvailable" runat="server" style="color: #FF0000" 
    Text="There is no available copy in the library now." Visible="False"></asp:Label>
<br />
<br />
</form>
</body>
</html>

我感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:1)

这就是你如何做到的:

1)操纵你的dropwonlist,使其值与数据库中的列名相同

<asp:DropDownList ID="ddlFilter" runat="Server">
<asp:ListItem>Search by title</asp:ListItem>
<asp:ListItem Text="Search by author" Value="author" />
<asp:ListItem Text="Search by item type" Value="type" />
<asp:ListItem Text="Search by publish year" Value="year" />    
</asp:DropDownList>

2)操纵你的sql数据源:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Items] WHERE ([Title] LIKE '%' + @Title + '%' or [author] LIKE '%' + @Filter+ '% or [type] LIKE '%' + @Filter+ '%' or [year] LIKE '%' + @Filter+ '%'') And


    <SelectParameters>
          <asp:FormParameter FormField="tSearchBox" Name="Title" Type="String" />
   <asp:ControlParameter Name="Filter" ControlID="ddlFilter" PropertyName="SelectedValue" /> 
     </SelectParameters>
 </asp:SqlDataSource>

如果你想要

,你可以改变或接近和

问候