Compartilhar via


CookieParameter.CookieName Propriedade

Definição

Obtém ou define o nome do cookie HTTP ao qual o parâmetro está associado.

public:
 property System::String ^ CookieName { System::String ^ get(); void set(System::String ^ value); };
public string CookieName { get; set; }
member this.CookieName : string with get, set
Public Property CookieName As String

Valor da propriedade

String

Uma cadeia de caracteres que identifica o cookie HTTP do lado do cliente ao qual o parâmetro está associado.

Exemplos

O exemplo de código a seguir demonstra como usar declarativamente um SqlDataSource controle e um CookieParameter objeto associado a um cookie HTTP para exibir dados do banco de dados Northwind Traders em um GridView controle.

<%@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>
<%@Page  Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
  ' These cookies might be added by a login form.
  ' They are added here for simplicity.
  If (Not IsPostBack) Then
      Dim cookie As HttpCookie

      cookie = New HttpCookie("lname","davolio")
      Response.Cookies.Add(cookie)

      cookie = New HttpCookie("loginname","ndavolio")
      Response.Cookies.Add(cookie)

      cookie = New HttpCookie("lastvisit", DateTime.Now.ToString())
      Response.Cookies.Add(cookie)
  End If
End Sub ' 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="<%$ 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>

O exemplo de código a seguir demonstra como criar um CookieParameter objeto programaticamente, definir suas propriedades e adicioná-lo à coleção de SelectParameters um SqlDataSource controle.

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);
        }
    }
}
Partial Class cookieparam2vb_aspx
    Inherits System.Web.UI.Page
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' These cookies might be added by a login form.
        ' They are added here for simplicity.
        If (Not IsPostBack) Then
            Dim cookie As HttpCookie

            cookie = New HttpCookie("lname", "davolio")
            Response.Cookies.Add(cookie)

            cookie = New HttpCookie("loginname", "ndavolio")
            Response.Cookies.Add(cookie)

            cookie = New HttpCookie("lastvisit", DateTime.Now.ToString())
            Response.Cookies.Add(cookie)


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

            SqlDataSource1.SelectParameters.Add(cookieParam)

        End If
    End Sub
End Class

Comentários

A CookieName propriedade identifica um cookie HTTP, que é representado por um HttpCookie objeto e está disponível por meio do objeto atual HttpRequest . Se o cookie HTTP não estiver disponível no objeto atual HttpRequest , o Evaluate método associará o parâmetro ao valor da DefaultValue propriedade, se ele estiver definido. Se a DefaultValue propriedade não estiver definida, o Evaluate método não associará o parâmetro a um valor.

Aplica-se a