Cookies can be used to send data from one webform to another. In general, web sites use cookies to store user preferences or other information that is client-specific. Cookies store small amounts of information on the client’s machine.
//File 1
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cookie Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Cookie Example</h3>
<label>Name :</label><br />
<asp:TextBox runat="server" ID="txtname"></asp:TextBox><br />
<label>Email :</label><br />
<asp:TextBox runat="server" ID="txtmail"></asp:TextBox><br /><br />
<asp:Button runat="server" ID="btnsubmit" OnClick="btnsubmit_Click" Text="Go to other webform" />
</div>
</form>
</body>
</html>
//File2
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cookie Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Cookie Example</h3>
<label>Name :</label> <asp:Label> runat="server" ID="lblname"></asp:Label><br />
<label>Email :</label> <asp:Label> runat="server" ID="lblemail"></asp:Label>
</div>
</form>
</body>
</html>
//File1 code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Training
{
public partial class CookieExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("UserInfo");
cookie["Name"] = txtname.Text;
cookie["Email"] = txtmail.Text;
//To sepcify expiry of cookie
cookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(cookie);
Response.Redirect("CookieEx2.aspx");
}
}
}
//File 2 Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Training
{
public partial class CookieEx2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["UserInfo"];
if(cookie != null)
{
lblname.Text = cookie["Name"];
lblemail.Text = cookie["Email"];
}
}
}
}
//File1
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Query String Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Query String Example</h3>
<label>Name :</label><br />
<asp:TextBox runat="server" ID="txtname"></asp:TextBox><br />
<label>Email :</label><br />
<asp:TextBox runat="server" ID="txtmail"></asp:TextBox><br /><br />
<asp:Button runat="server" ID="btnsubmit" OnClick="btnsubmit_Click" Text="Go to other webform" />
</div>
</form>
</body>
</html>
//File2
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Query String Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Query String Example</h3>
<label>Name :</label> <asp:Label> runat="server" ID="lblname"></asp:Label><br />
<label>Email :</label> <asp:Label> runat="server" ID="lblemail"></asp:Label>
</div>
</form>
</body>
</html>
//File 1 Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Training
{
public partial class QueryString1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
Response.Redirect("QueryString2.aspx?Name="+txtname.Text+"&Email="+txtmail.Text+"");
}
}
}
//File2 Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Training
{
public partial class QueryString2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblname.Text = Request.QueryString["Name"];
lblemail.Text = Request.QueryString["Email"];
}
}
}
//Front-end code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Text Changed Event Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Text Changed Event Example</h3>
<label>Enter Name :</label><br />
<asp:TextBox runat="server" ID="txtname" OnTextChanged="txtname_TextChanged" AutoPostBack="true"></asp:TextBox><br />
<label>You have entered :</label><br />
<asp:Label runat="server" ID="lblname"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Training
{
public partial class TextChangedEx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtname_TextChanged(object sender, EventArgs e)
{
lblname.Text = txtname.Text;
txtname.Text = "";
}
}
}