Various state management techniques
A. Client-side State Management:
A. Client-side State Management:
- View State
- Hidden Field
- Cookies
- Control State
B. Server-side State Management
- Session
- Application Object
- Caching
1. View state
View State is a technique to maintain the state of controls during page post-back, meaning it stores the page value at the time of post-back (sending and receiving information from the server) of your page and the view state data can be used when the page is posted back to the server and a new instance of the page is created.
Sample Code
- namespace Test
- {
- public partial class _Default : Page
- {
- int TestCounter = 0;
- protected void Page_Load(object sender, EventArgs e)
- {
- if(!IsPostBack)
- {
- TextBox1.Text = "0";
- TextBox2.Text = "0";
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- //With out View State
- TestCounter = TestCounter + 1;
- TextBox1.Text = TestCounter.ToString();
- if (ViewState["counter"] != null)
- {
- TestCounter = (int)ViewState["counter"] + 1;
- TextBox2.Text = TestCounter.ToString();
- }
- ViewState["counter"] = TestCounter;
- }
- }
- }
View State advantages:
- Very easy to implement.
- Stored on the client browser in a hidden field as a form of Base64 Encoding String not encrypted and can be decoded easily.
- Good with HTTP data transfer
View State disadvantages:
- The performance overhead for the page is larger data stored in the view state.
- Stored as encoded and not very safe to use with sensitive information.
Session State :-
in session state one we stored value in session then we can use it on another page but in view state we can not use stored value on another page.
- //Stored Textbox value
- Session["Counter"] = TextBox3.Text;
- //Stored Dataset
- Session["ds"] = dsData;
- Session variables are stored in a SessionStateItemCollection object that is exposed through the HttpContext.Session property.
0 Comments:
Post a Comment