Showing posts with label Asp.Net State Management. Show all posts
Showing posts with label Asp.Net State Management. Show all posts

Friday, 21 October 2016

ASP.Net State Management

ASP.Net State Management
                   We know any web page are state less, means when we post a page to server , server will not return the same state as it was send . The reason is , when we post a form /page to the server , server process the page and generate new html and send to browser .For example a web page is taking student information , student name , date of birth ,country ,state .We enter student name , date of birth , now we have Select student country “Say” India , page need to go server and get state list of India .When it posted to server for state information , the whole html is created again and when comes to browser , student name , date of birth not data is not there .Input fields are empty.

                To over come this problem , Asp.Net introduced State Management . State Management is a technique to keep data while post back occur , that means , when student page with student data will be posted to the server , and server will return  student page with student data.

              There are several way to maintain State in ASP.Net , such as View State ,Hidden Fields ,
Cookies ,  Query String , Control State . Some are Clint Site and Some are Server Site.


  View State : View state is the technique that the ASP.NET page  uses to preserve page and control  values when post back . The working of View State can be controlled by C# code , by default view state enabled . View state is a hidden filed with serialized into base64-encoded strings.We van enable /disable code from page level


%@ Page Language="C#"  CodeFile="Default2.aspx.cs" Inherits="Default2"  EnableViewState="false"

We can encrypt view state


%@ Page Language="C#"  CodeFile="Default2.aspx.cs" Inherits="Default2" EnableViewState="true ViewStateEncryptionMode="Always" 

There is also Option for enable/disable at control level

asp:TextBox EnableViewState=true

We can also do enable/disable  the whole page and enable/disable on or more control.
We can also use View State variable as follow


protected void Page_Load(object sender, EventArgs e)

    {

        if (ViewState["NameOfStudent"] != null)

            txtStudentName.Text = ViewState["NameOfUser"].ToString();

        else

            txtStudentName.Text = "Not set yet...";

    } 

    protected void SubmitForm_Click(object sender, EventArgs e)

    {
        ViewState["NameOfUser"] = txtStudentName.Text;
        txtStudentName.Text = NameField.Text;
    }
 

We can see view state value , to see the value , please write click on mouse and go to "View source" , here is the example
Hidden field : Hidden field is control , which is not displayed in the front end .This field are use to hold control value , here is an example


<body>
    <form id="form1" runat="server">
    <div>
      <asp:HiddenField ID="hiddn_1" runat="server" />
      <asp:TextBox ID="txtStudent" runat="server" ></asp:TextBox>
        <asp:Button ID="Button1" runat="server" oncommand="Button1_Command"
            Text="Button" />
        <br />
    </div>
    </form>
</body>

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtStudent.Text = hiddn_1.Value;
    }
    protected void Button1_Command(object sender, CommandEventArgs e)
    {
        hiddn_1.Value = txtStudent.Text;
    }
}

Here , hidden field hold the value of "txtStudent" , while page load , the process  is reverse

Cookies : Cookies are small text to stored some value .This is like key pair combination.
Cookies are associated with a Web site, not with a specific page, so the browser and server will exchange cookie information no matter what page the user requests from your site.

 protected void Page_Load(object sender, EventArgs e)
    {       
     Response.Cookies["LoggedUsere"].Value = "MyUser";
     Response.Cookies["LoggedUser"].Expires = DateTime.Now.AddDays(1);
    }
    protected void Button1_Command(object sender, CommandEventArgs e)
    {
        HttpCookie obj = new HttpCookie("LoggedUser");
        string loggedUser = Request.Cookies["LoggedUser"].Values.ToString();

    }

Here ,  user values stored in cookies and later retrieved in a sting.

Context.Handler
Context handler just return the immediate previous web page when Server.transfer occure , this does not works for Response.redirect.


public partial class MyContext1 : System.Web.UI.Page
{
    internal string value
    {
        get
        {
            return TextBox1.Text;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("MyContext1.aspx");
    }
}

public partial class MyContext2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MyContext1 largePare;
        largePare = (MyContext1)Context.Handler;
        this.textBox1.Text = largePare.value;
    }
}

Query String 
       This is popularly used in ASP.Net Application . A  string of query can be generated before posting and later while loading field value can be retrive from query .this works for both Server.Transfer and Request.Response.


   protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("MyContext1.aspx?mText=20");
    }
 

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["mText"] != null)
        {
            this.Textbox1.Text = Request.QueryString["mText"].ToString();
        }
    }

Session State
Asp.net offer a unique key to each user when a new session begin , this is called session id/ Session key.This key is stored is cookie.In each request client send the key to Server .Session State maintained by Globax.aspx


<%@ Application Language="C#" %>

<script runat="server">

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        Session["MyNumber"] = 100;
    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }
      
</script>

Application State
As ASP.Net is stateless protocal ,it is application state which can hold some value , we can use during page load event to show the value to client .Application variable inisialised when application starts only.


<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["MyAppVarable"] = "100";

    } 
</script>

Catching
Catching is a technique to deliver the same Web Page which is frequently   used without rebuild the page HTML.For I am using page A , after 3 second I called same page A to browser , server will return page A to browse from cache memory.But the question is how many time cache hold the page , which page will hold by cache , there are several caching technique output caching , data catching ,object catching.

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...