所以,我有一个select查询,我从一个页面上的下拉列表中选择值,并在SELECT查询中使用它。我遇到的问题是,当我从一个页面导航到下一个页面时,cookie不会在查询中使用。如果有人能弄清楚原因,那就太棒了。
我的两个页面的代码如下:
主页
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [ProductName] FROM [Products]"></asp:SqlDataSource>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="ProductName" DataValueField="ProductName">
</asp:DropDownList>
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/infopage.aspx">Information Screen</asp:LinkButton>
<br />
<asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl="~/Orders.aspx">Orders Screen</asp:LinkButton>
<br />
<asp:LinkButton ID="LinkButton3" runat="server" PostBackUrl="~/Items.aspx">Items Screen</asp:LinkButton>
<br />
<asp:LinkButton ID="LinkButton4" runat="server" PostBackUrl="~/Totals.aspx">Total Screen</asp:LinkButton>
</div>
</form>
</body>
</html>
第一个链接页面:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="infopage.aspx.vb" Inherits="infopage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [Discontinued], [ReorderLevel] FROM [Products] WHERE [ProductName] = @p1">
<SelectParameters>
<asp:CookieParameter CookieName="product" Name="p1" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="None" Height="50px" Width="125px">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<EditRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<Fields>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" SortExpression="ReorderLevel" />
</Fields>
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
</asp:DetailsView>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Default.aspx">Go back</asp:LinkButton>
<br />
<br />
</div>
</form>
</body>
</html>
主页代码隐藏:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub LinkButton1_Click(sender As Object, e As EventArgs) Handles LinkButton1.Click
Dim product As New HttpCookie("product")
product.Value = DropDownList1.SelectedValue
product.Expires = DateTime.Now.AddHours(1)
Response.Cookies.Add(product)
End Sub
End Class