Thursday, August 17, 2023

Application state , View State , Cookies

 


ASP.Net Application State

ASP.Net Application state is a server side state management technique.Application state is a global storage mechanism that used to stored data on the server and shared for all users, means data stored in Application state is common for all user. Data from Application state can be accessible anywhere in the application. Application state is based on the System.Web.HttpApplicationState class.

 

Syntax of Application State

Create  application state

·         Application[“varible”] = control;

Retrieve information from application state

·         Control = Application[“varible”].ToString();

 

Example of Application State in ASP.Net

Generally we use application state for calculate how many times a given page has been visited by various clients.

 

 

Coading  for button_click  event

 

protected void button_Click(object sender, EventArgs e)

    {

        int count = 0;

 

        if (Application["Visit"] != null)

        {

            count = int.Parse(Application["Visit"].ToString());

        }

 

        count = count + 1;

        Application["Visit"] = count;

        Label1.Text = "Total Visit = " + count.ToString();

       

    }

 

 

 

 

 

 

Run Program

1.    Run  program

2.    Copy URL 

3.    Past URL  to  new  tab  on browser

Result

Label show  number of count to visit  user

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ViewState Example in ASP.Net

ViewState is a important client side state management technique. ViewState is used to store user data on page at the time of post back of web page.

ViewState does not hold the controls, it holds the values of controls. It does not restore the value to control after page post back. ViewState can hold the value on single web page, if we go to other page using response.redirect then ViewState will be null.

 

·         ViewState stores data on single page

·         ViewState is client side state management technique

·         Session stores data on whole website pages

·         Session is a server side state management technique

 

Viewstate Example

Store the value in viewstate

ViewState[“varible”]=control;

 

Retrieve information from viewstate

control=ViewState[“varible”].ToString();

 

 

C# Code for above example

 protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnclear_Click(object sender, EventArgs e)
    {
        ViewState["name"] = TextBox1.Text;
        
         TextBox1.Text = "";
    }
    protected void btndisplay_Click(object sender, EventArgs e)
    {
        Label1.Text = ViewState["name"].ToString();
    }

 

 

 

Run Program

1.  First write any data in textbox

2.   After that click clear value button

3.   After that click  display value button

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ASP.Net Cookie Example

Cookies is a small pieces of text information which is stored on user hard drive using users browser for identify users. It may contain username, ID, password or any information. Cookie does not use server memory.

 

How  to create Cookies

Response.Cookies["varible"].Value = control;

 

 

 

How  to expire  cookies

 

Response.Cookies["varible"].Expires= DateTime.Now.AddMinutes(1);

 

 

C# code for Cookie Example

Create Cookie Button C# Code

protected void btncreate_Click(object sender, EventArgs e)
    {
       Response.Cookies["name"].Value = Textbox1.Text;
       Response.Cookies["name"].Expires = DateTime.Now.AddMinutes(1);
       Label1.Text = "Cookie Created";
       Textbox1.Text = "";
    }   

 

 

 

 

Retrieve Cookie Button Code

protected void btnretrieve_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["name"] == null)
        {
            Textbox2.Text = "No cookie found";
        }
        else
        {
            Textbox2.Text = Request.Cookies["name"].Value;
        }
    }

 

 

 

 

No comments:

Post a Comment

GRIDVIEW ON ROW DATA BOUND EVENT

 Database Create  Student : roll , name , city , cost  Fix 6 Value  in Database Record  ====================================================...