This documentation is archived and is not being maintained.

CookieParameter.CookieName Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets the name of the HTTP cookie that the parameter binds to.

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

public string CookieName { get; set; }
/** @property */
public String get_CookieName ()

/** @property */
public void set_CookieName (String value)

public function get CookieName () : String

public function set CookieName (value : String)

Property Value

A string that identifies the client-side HTTP cookie that the parameter binds to.

The CookieName property identifies an HTTP cookie, which is represented by an HttpCookie object and is available through the current HttpRequest object. If the HTTP cookie is not available in the current HttpRequest object, the Evaluate method binds the parameter to the value of the DefaultValue property, if it is set. If the DefaultValue property is not set, the Evaluate method fails to bind the parameter to a value.

The following code example demonstrates how to declaratively use a SqlDataSource control and a 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>

The following code example demonstrates how to programmatically create a CookieParameter object, set its properties, and add it to a SqlDataSource control's SelectParameters collection.

public partial class cookieparam2cs_aspx : System.Web.UI.Page 
{
    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()));


            // You can add a CookieParameter to the SqlDataSource control's
            // SelectParameters collection programmatically.
            CookieParameter cookieParam = new CookieParameter();
            cookieParam.Name = "lastname";
            cookieParam.Type = TypeCode.String;
            cookieParam.CookieName = "lname";

            SqlDataSource1.SelectParameters.Add(cookieParam);
        }

    }
}

CookieParameter cookieParam = new CookieParameter();
cookieParam.set_Name("lastname");
cookieParam.set_Type(System.TypeCode.String);
cookieParam.set_CookieName("lname");

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
Show: