InterviewSolution
| 1. |
What are the different state management techniques in .NET? |
|
Answer» State management basically stores the information of user/Page or object when a user makes a REQUEST on a web page, since HTTP is a protocol which doesn’t store the information during each request made by the user.so Asp.net state management is used to preserve the data.
Server Side:- 1) Session: when we want to store the data of web page over multiple requests we use session. In this TECHNIQUE we can store any kind of information or object in server memory it basically stores the information. The session is user specific since it stores the information of user separately. When we store data to Session state, a session cookie named is ASP.NET_SessionId is created automatically. Example: Storing the data in Session object Session ["UserName"] = txtName.Text;Retrieving the data from the Session object Label1.Text = Session ["UserName"].ToString();Session timeout is 20 min if we want to increase the timeout of session then we need to configure the web config <configuration> <system.web> <sessionState timeout="60" /> </system.web> </configuration>2). Application object: In application object, we can store the information at the application level rather than User level, where every user can access that information .the main disadvantage is that sometimes there is a concurrency problem, so for that, we use a lock and unlock method. So if multiple requests came for same data then only one thread can do work. Example: Application["Message"] = "Hello to all";We can use an application object to count the number of VISITORS on that website. Client Side: 1) Query Strings: When we want to transfer the data over the pages from the URL then we use query string, it cannot HANDLE the huge data and it is compatible with all browsers, we cannot transfer the objects and controls in query strings, so it is not meant for transferring sensitive data through query string. Example: Response.Redirect( "SecondPage.aspx?Type=01&Query=Query1" );Or if we want to use query string to transfer the data then we can encrypt the sensitive data and we can transfer the data. 2). Control State: It is used to transfer the data which is stored in controls .it is just like same as view state but here we can store the data of the controls like dropdown list`s selected items, we cannot disabled the control state if viewstate is disabled then control state works same as view state. 3). View State: It is one of the finest methods to preserve the data, in view state we can store any kind of data like controls data, and even variables and raw data, view state encrypt the data when it will render to the user, so if user wants to see the value of view state in view source it will contain the symbols only which are very hard to decrypt. The only disadvantage is that page contains much space over the page. To handle the view state we just need to "EnableViewState = true" attribute to any control to save its state, Example: public class Person { public string FirstName {get;set;} public string LastName {get;set;} } public partial class default : Web.UI.Pages { Person p = new Person(); p.FirstName = "Harshit"; p.LastName = "Mittal"; ViewState["PersonDetails"]=p; }4). Cookies: Cookies are used to identify the user on a web page whenever the user makes the request for the first time it stores that request data in the cookies and when the user visit that web page for the second time then browser check the cookies data with second request data made by the user if the result matches browser to consider them as a previous user else consider a new user. Example: The Case of Remember me on every login page. There are 2 types of cookies:
Example: Response.Cookies["nameWithPCookies"].Value = "This is A Persistence Cookie"; Response.Cookies["nameWithPCookies"].Expires = DateTime.Now.AddSeconds(10);
Example: Response.Cookies["nameWithNPCookies"].Value = "This is A Non Persistence Cookie";5) Hidden Field - When we want to store the small amount of data or we can say the if want to save the single value, hidden field controls is not rendered on the client browser, it is invisible to the browser. Example: <asp:HiddenField ID="HiddenField1" runat="server" />At the server code protected void Page_Load(object sender, EventArgs e) { if (HiddenField1.Value != null) { int val= Convert.ToInt32(HiddenField1.Value) + 1; HiddenField1.Value = val.ToString(); Label1.Text = val.ToString(); } } |
|