Export (0) Print
Expand All

CookieParameter Class

Note: This class is new in the .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.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class CookieParameter : Parameter
public class CookieParameter extends Parameter
public class CookieParameter extends Parameter

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.

NoteImportant:

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="C#" %>
<!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, 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>
<HTML>
  <BODY>
    <FORM runat="server">

      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataSet"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          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>

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

System.Object
   System.Web.UI.WebControls.Parameter
    System.Web.UI.WebControls.CookieParameter

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft