ViewState for state management
v ViewState :-
I. It is used to store data value in a single webpage.
II. The ViewState cannot transfer the data between one page to another page .
v How to create ViewState?
Sytanx:- ViewState[“var”]=Control;
Ex.- ViewState[“Email”]=Textbox1.text;
v How to access ViewState data value?
Syntax:-control=ViewState[“var”].Tostring();
Ex:-Label1.text=ViewState[“Email”].tostring();
Code:-using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class stateview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// create viewstate
ViewState["msg"] = TextBox1.Text;
TextBox1.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
//access the ViewState
Label1.Text = ViewState["msg"].ToString();
}
}
}
v COOKIES:-
I. Cookies is used to store pieces of information .
II. There are two type of implementation by which cookies can be used.
a. Create Cookies.
b. Expires Cookies.
A. Create Cookies:-
Syntax:- Response.Cookies[“var”].value=control;
Ex- Response.Cookies[“msg”].value=textbox1.text;
B. Expires Cookies:-
Syntax:- Response.Cookies[“var”].Expires=Datetime.now.AddMinutes(1);
Ex.-Response.Cookies[“msg”].Expires=Datetime.now.AddMinutes(2);
n How to access Cookies?
Syntax:- Control=Request.Cookies[“var”].value;
Ex- label1.text=Request.Cookies[“email”].value;
Comments
Post a Comment