如何显示基于在asp.net下拉列表上gridview的结果吗? [英] How to display results on gridview based on a dropdown list in asp.net?

查看:86
本文介绍了如何显示基于在asp.net下拉列表上gridview的结果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个搜索页面,并显示结果。下面是用户界面:

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

我显示在一个GridView的结果。现在,我只按标题搜索,但我在下拉列表中的其他选项。我想写的基础上的下拉列表中选择值查询,然后在GridView中显示出来。我怎样才能做到这一点?这里是code搜索页面:

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:

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

下面是code为我显示结果的页面:

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>

我AP preciate任何帮助。谢谢

I appreciate any help. Thanks

推荐答案

那么这里为u如何做到这一点:

well here is how u do it:

1)maniplulate您dropwonlist像,使得这些值是相同的列名的数据库

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

<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数据源像这样:

2) manipulate your sql datasource as so:

<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

关于

这篇关于如何显示基于在asp.net下拉列表上gridview的结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆