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

时间:2021-05-01 21:00:07

I am making a search page and displaying results. Here is the UI:

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

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

I display the results on a gridview. For now, i only search by title, but i have other options in dropdown list. I want to write queries based on the selected value of the dropdown list and then display them in gridview. How can i do this? Here is the code for the search page:

我在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>

Here is the code for the page that i display results:

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

<%@ 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>

I appreciate any help. Thanks

我感谢任何帮助。谢谢

1 个解决方案

#1


1  

well here is how u do it:

以下是你怎么做的:

1) maniplulate your dropwonlist like so that the values are the same as column names in the database

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) manipulate your sql datasource as so:

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>

and you can change the Or close to And if you want

如果你愿意,你可以改变或接近和

regards

问候

#1


1  

well here is how u do it:

以下是你怎么做的:

1) maniplulate your dropwonlist like so that the values are the same as column names in the database

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) manipulate your sql datasource as so:

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>

and you can change the Or close to And if you want

如果你愿意,你可以改变或接近和

regards

问候