14.6. State Management and ASP.NET AJAX

The nice thing with ASP.NET AJAX is that you now have the ability to record state back to the server in a quick and little request. Before AJAX, you would have to make an entire post of the page and send back the entire page in the response just to write something to a Session object or some other aspect of recording state.

To provide an example of this, Listing 14-8 shows an ASP.NET AJAX page that is working with a Session object on the server and changing aspects of the page using an UpdatePanel server control.

Example 14-8. Working with a Session object from an ASP.NET AJAX page
<%@ Page Language="C#" %>

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        int resultCount = int.Parse(Session["count"].ToString());
        resultCount += 1;
        Session["count"] = resultCount.ToString();
        Label1.Text = "Hello " + TextBox1.Text;
        Label2.Text = "You have put in your name " + Session["count"] + " times.";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
Session["count"] = 0; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Working with Sessions</title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Button ID="Button1" runat="server" Text="Submit your name numerous ...

Get Professional ASP.NET 3.5 AJAX now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.