Friday, 27 December 2013

How to pass values from one page to another page in C# ASP.NET?


You can use ViewState to maintain the state of controls during page postback.

For setting Viewstate :-
ViewState["myName"] = "xyz";

For retrieving Viewstate on postback:
string myName = ViewState["myName"].ToString();


You can use Cookies to save information on client side.

For setting Cookies :-

HttpCookie cName = new HttpCookie("Name");
cName.Value = txtName.Text;
Response.Cookies.Add(cName);
Response.Redirect("newPage.aspx");


You can use the Context to Transfer data to the one page to another.

For setting Context :-

Page1.aspx.cs
this.Context.Items["fName"] = "xyz";

Page2.aspx.cs
string fName = this.Context.Items["fName"].ToString();


You can use the Session Variable to preserve the common data which is required almost for every page or across the application for the specific user.

For setting Session:
Session["fName"] = "xyz";

For retrieving Session from any page :
string fName = Session["fName"].ToString();//default session timeout is 20 minutes
That’s it!!…..Happy Programming...

No comments:

Post a Comment