CookieParameter.CookieName 属性
定义
获取或设置参数绑定到的 HTTP Cookie 的名称。Gets or sets the name of the HTTP cookie that the parameter binds to.
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
属性值
一个字符串,该字符串标识参数绑定到的客户端 HTTP Cookie。A string that identifies the client-side HTTP cookie that the parameter binds to.
示例
下面的代码示例演示如何以声明方式使用 SqlDataSource 控件和 CookieParameter 绑定到 HTTP cookie 的对象在控件中显示 Northwind 商贸数据库中的数据 GridView 。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 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>
下面的代码示例演示如何以编程方式创建 CookieParameter 对象,设置其属性,并将其添加到 SqlDataSource 控件的 SelectParameters 集合中。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);
}
}
}
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
注解
CookieName属性标识 HTTP cookie,它由对象表示, HttpCookie 可通过当前 HttpRequest 对象使用。The CookieName property identifies an HTTP cookie, which is represented by an HttpCookie object and is available through the current HttpRequest object. 如果 HTTP cookie 在当前对象中不可用 HttpRequest ,则 Evaluate 方法会将参数绑定到属性的值 DefaultValue (如果已设置)。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. 如果 DefaultValue 未设置该属性,则该 Evaluate 方法无法将参数绑定到值。If the DefaultValue property is not set, the Evaluate method fails to bind the parameter to a value.