CookieParameter.CookieName 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定參數繫結至的 HTTP Cookie 名稱。
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。
範例
下列程式碼範例示範如何以宣告方式使用 SqlDataSource 控制項和 CookieParameter 系結至 HTTP Cookie 的物件,以顯示控制項中 GridView Northwind Traders 資料庫中的資料。
<%@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 集合。
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,此 Cookie 是由 HttpCookie 物件表示,而且可透過目前的 HttpRequest 物件取得。 如果目前 HttpRequest 物件中無法使用 HTTP Cookie,則如果已設定,方法會將 Evaluate 參數系結至 屬性的值 DefaultValue 。 DefaultValue如果未設定屬性, Evaluate 方法就無法將參數系結至值。