CookieParameter Class
Assembly: System.Web (in system.web.dll)
You can use the CookieParameter class to bind the value of a client-side HTTP cookie passed as part of an HTTP request to a parameter used by ASP.NET data source controls.
The CookieParameter class provides the CookieName property, which identifies the name of the HttpCookie object to bind to, in addition to those inherited from the Parameter class. The CookieParameter class attempts to bind to the named cookie every time the Evaluate method is called.
Important: |
|---|
| Controls that bind data to the parameter might throw an exception if a CookieParameter object is specified, but no corresponding cookie is passed with the HTTP request. Similarly, they might display no data if the cookie is passed with a null reference (Nothing in Visual Basic). Set the DefaultValue property to avoid these situations where appropriate. |
The following code example demonstrates how to use a SqlDataSource control and CookieParameter object bound to an HTTP cookie to display data from the Northwind Traders database in a GridView control.
<%@Page Language="VJ#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, System.EventArgs e)
{
// These cookies might be added by a login form.
// They are added here for simplicity.
if (!get_IsPostBack()) {
get_Response().get_Cookies().Add(new HttpCookie(
"lname", "davolio"));
get_Response().get_Cookies().Add(new HttpCookie(
"loginname", "ndavolio"));
get_Response().get_Cookies().Add(new HttpCookie(
"lastvisit", System.DateTime.get_Now().ToString()));
}
}//Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
SelectCommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,ShippedDate
FROM Orders WHERE EmployeeID =(SELECT EmployeeID FROM Employees WHERE LastName = @lastname)">
<SelectParameters>
<asp:CookieParameter Name="lastname" CookieName="lname" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView
id="GridView1"
runat="server"
AllowSorting="True"
DataSourceID="SqlDataSource1">
</asp:GridView>
</form>
</body>
</html>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
Important: