Hi Prasunjeet,
I have created sample with master page. Please refer the below sample code.
Instead of Response.SetCookie use Response.Cookies.Add
SetCookie is used for Updating an existing cookie in the cookie collection.
Cookies.Add is used for Adding specific cookie to the cookie collection.
MasterPage.master
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
Master Page
<hr />
<asp:Button Text="Get Cookie" runat="server" OnClick="GetCookie" />
<br />
Cookie value is:
<asp:Label ID="lblGetCookieValue" runat="server" />
<hr />
<br />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
MasterPage.master.cs
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GetCookie(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["PdGuestID"];
if (cookie != null)
{
lblGetCookieValue.Text = Request.Cookies["PdGuestID"].Value;
}
}
}
CS.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
Child Page
<hr />
<asp:TextBox ID="txtCookie" runat="server" />
<asp:Button Text="Set Cookie" runat="server" OnClick="SetCookie" />
<hr />
</asp:Content>
CS.aspx.cs
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SetCookie(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("PdGuestID");
cookie.Value = txtCookie.Text.Trim();
cookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(cookie);
}
}
Screenshot