我正在制作一个搜索页面并显示结果。这是用户界面:
我在网格视图上显示结果。目前,我只按标题搜索,但下拉列表中有其他选项。我想根据下拉列表的选定值编写查询,然后在 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:
<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>
<asp:Button ID="bSearchButton" runat="server" Height="24px"
onclick="bSearchButton_Click" PostBackUrl="SearchResults.aspx" Text="Search"
Width="119px" />
<br />
<br />
<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>
<asp:TextBox ID="tLoanBox" runat="server" Width="137px"></asp:TextBox>
<asp:Button ID="bLoanButton" runat="server" onclick="bLoanButton_Click"
PostBackUrl="~/Pages/SearchResults.aspx" Text="Loan" Width="103px" />
<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>
我很感激任何帮助。谢谢