System.Web.UI.WebControls Namespace
.NET Framework version 2.0
Binds the value of a client-side HTTP cookie to a parameter object. The parameter can be used in a parameterized query or command to select, filter, or update data.
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 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.
<script language = "C#" runat = "server">
void Page_Load(Object src, EventArgs e){
// These cookies might be added by a login form.
// They are added here for simplicity.
if ( !IsPostBack ) {
Response.Cookies.Add ( new HttpCookie ( "lname", "davolio" ) );
Response.Cookies.Add ( new HttpCookie ( "loginname", "ndavolio" ) );
Response.Cookies.Add ( new HttpCookie ( "lastvisit", DateTime.Now.ToString ( ) ) );
}
}
</script>
<asp:sqldatasource id = "SalesByEmployee" runat = "server"
SelectCommand = "SELECT OrderID, OrderDate, CustomerID, ShipAddress, ShipCity, ShipCountry
FROM Orders
WHERE EmployeeID = ( SELECT EmployeeID FROM Employees WHERE LastName = @lastname )"
connectionstring = "<%$ ConnectionStrings:aspnet %>">
<selectparameters>
<asp:cookieparameter name = "lastname" cookiename = "lname" />
</selectparameters>
</asp:sqldatasource>
<asp:gridview id = "ordersGrid" runat = "server"
width=90% cellpadding=3 font-size=8pt
datasourceid = "SalesByEmployee"
allowsorting allowpaging>
<headerstyle backcolor = "lightsteelblue" height=20pt />
<alternatingrowstyle backcolor = "whitesmoke" />
</asp:gridview>
Show me
Using Parameters with Data Source Controls