Export (0) Print
Expand All

CookieParameter Class

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.

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

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

public class CookieParameter : Parameter

The CookieParameter type exposes the following members.

  NameDescription
Public methodCookieParameter()Initializes a new unnamed instance of the CookieParameter class.
Protected methodCookieParameter(CookieParameter)Initializes a new instance of the CookieParameter class with the values of the instance specified by the original parameter.
Public methodCookieParameter(String, String)Initializes a new named instance of the CookieParameter class, using the specified string to identify which HTTP cookie to bind to.
Public methodCookieParameter(String, DbType, String)Initializes a new instance of the CookieParameter class that has the specified name and database type and that is bound to the specified HTTP cookie.
Public methodCookieParameter(String, TypeCode, String)Initializes a new named and strongly typed instance of the CookieParameter class, using the specified string to identify which HTTP cookie to bind to.
Top

  NameDescription
Public propertyConvertEmptyStringToNullGets or sets a value indicating whether the value that the Parameter object is bound to should be converted to null if it is String.Empty. (Inherited from Parameter.)
Public propertyCookieNameGets or sets the name of the HTTP cookie that the parameter binds to.
Public propertyDbTypeGets or sets the database type of the parameter. (Inherited from Parameter.)
Public propertyDefaultValueSpecifies a default value for the parameter, should the value that the parameter is bound to be uninitialized when the Evaluate method is called. (Inherited from Parameter.)
Public propertyDirectionIndicates whether the Parameter object is used to bind a value to a control, or the control can be used to change the value. (Inherited from Parameter.)
Protected propertyIsTrackingViewStateGets a value indicating whether the Parameter object is saving changes to its view state. (Inherited from Parameter.)
Public propertyNameGets or sets the name of the parameter. (Inherited from Parameter.)
Public propertySizeGets or sets the size of the parameter. (Inherited from Parameter.)
Public propertyTypeGets or sets the type of the parameter. (Inherited from Parameter.)
Public propertyValidateInputGets or sets a value that specifies whether the parameter's value is validated.
Protected propertyViewStateGets a dictionary of state information that allows you to save and restore the view state of a Parameter object across multiple requests for the same page. (Inherited from Parameter.)
Top

  NameDescription
Protected methodCloneReturns a duplicate of the current CookieParameter instance. (Overrides Parameter.Clone().)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodEvaluateUpdates and returns the value of the CookieParameter object. (Overrides Parameter.Evaluate(HttpContext, Control).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetDatabaseTypeGets the DbType value that is equivalent to the CLR type of the current Parameter instance. (Inherited from Parameter.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodLoadViewStateRestores the data source view's previously saved view state. (Inherited from Parameter.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnParameterChangedCalls the OnParametersChanged method of the ParameterCollection collection that contains the Parameter object. (Inherited from Parameter.)
Protected methodSaveViewStateSaves the changes to the Parameter object's view state since the time the page was posted back to the server. (Inherited from Parameter.)
Protected methodSetDirtyMarks the Parameter object so its state will be recorded in view state. (Inherited from Parameter.)
Public methodToStringConverts the value of this instance to its equivalent string representation. (Inherited from Parameter.)
Protected methodTrackViewStateCauses the Parameter object to track changes to its view state so they can be stored in the control's ViewState object and persisted across requests for the same page. (Inherited from Parameter.)
Top

  NameDescription
Explicit interface implemetationPrivate methodICloneable.CloneReturns a duplicate of the current Parameter instance. (Inherited from Parameter.)
Explicit interface implemetationPrivate propertyIStateManager.IsTrackingViewStateInfrastructure. Gets a value indicating whether the Parameter object is saving changes to its view state. (Inherited from Parameter.)
Explicit interface implemetationPrivate methodIStateManager.LoadViewStateInfrastructure. Restores the data source view's previously saved view state. (Inherited from Parameter.)
Explicit interface implemetationPrivate methodIStateManager.SaveViewStateInfrastructure. Saves the changes to the Parameter object's view state since the time the page was posted back to the server. (Inherited from Parameter.)
Explicit interface implemetationPrivate methodIStateManager.TrackViewStateInfrastructure. Causes the Parameter object to track changes to its view state so they can be stored in the control's ViewState object and persisted across requests for the same page. (Inherited from Parameter.)
Top

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 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 null. 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 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="<%$ 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>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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