Thursday 13 February 2014

client side State management in asp.net

1) Cookie

    -> small pieces of information stored on the client computer
    -> store user preferences or other information
    -> store only character data and limited to 4k in size
    -> it is simple
     -> cookies can be disabled on user browsers
     ->following code for add Cookie and retrieve Cookie data
------------------------------------------------------------------------------------------------

       

 HttpCookie c1 = new HttpCookie("ddd");
        c1.Values.Add("user", TextBox1.Text);//add Cookie value
        c1.Values.Add("cn", "ind");//add Cookie value

        Response.Cookies.Add(c1);


        Label1.Text = c1.Value.ToString();//retrieve Cookie data


2) Query string

    ->  it is very simple
    -> pass data one page to another page
    -> you can send data as part of URL
    -> do not require server resources
    -> Query string is directly visible to the user client browser
    -> 255 character limit in address bar
    ->following code for Query string
------------------------------------------------------------------------------------------------

       -> this code for set data using Query string

      

Response.Redirect("hidden_field.aspx?id="+TextBox1.Text+"&name="+TextBox2.Text);

// and this code for get data from Query string in another page

Label3.Text = Request.QueryString["id"];
Label3.Text += "  : " + Request.QueryString["name"];

   
3) view state

    -> You can programmatically store and retrieve data from view sate
    -> following code for View state
------------------------------------------------------------------------------------------------



       // Set data in Viewstate     

        ViewState["name"] = TextBox2.Text;


        // Get data from Viewstate in label

        Label1.Text = ViewState["name"].ToString();




4) hidden field

    -> you can set it's property
    -> hidden fields values are also posted to server
    -> simple to implement
    -> can store small amount data so that take less size
    -> following code for Hidden field
------------------------------------------------------------------------------------------------

        // Set data in Hidden field

        Label1.Text = HiddenField1.Value;

       
        // Get data from Hidden field in label

        Label2.Text = TextBox1.Text;

No comments:

Post a Comment



Asp.net tutorials