title: Setting Session Variable from UpdatePanel
body: <p>I am using ASP.NET 2.0 AJAX Extensions 1.0 with the version v1.0.20229 of the AJAX Control Toolkit (which to my knowledge is the latest for .NET 2.0/Visual Studio 2005).</p>
  
  <p>My web page (aspx) has a DropDownList control on an UpdatePanel.  In the handler for the DropDownList's SelectedIndexChanged event I attempt to set a session variable.</p>
  
  <p>The first time the event is fired, I get a Sys.WebForms.PageRequestManagerParserErrorException: "The message received from the server could not be parsed".  If I continue, subsequent SelectedIndexChanged's are handled successfully.</p>
  
  <p>I have stumbled upon a solution whereby if I initialise the session variable in my Page_Load (so the event handler is just setting the value of a session variable that already exists as opposed to creating a new one) the problem goes away.</p>
  
  <p>I'm happy to do this, but I'm curious as to exactly what the underlying cause is.  Can anyone explain?</p>
  
  <p>(My suspicion is that setting the session variable receives a response from the server which is then returned to the 'caller', but it's not the sort of response it knows how to deal with causing the exception?)</p>
  
  <p>EDIT: I reproduced the problem in a seperate little project:</p>
  
  <p><strong>Default.aspx</strong></p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SessionTest._Default" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;    
          &lt;asp:ScriptManager ID="ScriptManager1" runat="server" /&gt;
  
          &lt;div&gt;
  
              &lt;asp:UpdatePanel id="upCategorySelector" runat="server"&gt;
                  &lt;ContentTemplate&gt;
  
                      Category:
                      &lt;asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCategories_SelectedIndexChanged"&gt;
                          &lt;asp:ListItem&gt;Item 1&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Item 2&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem&gt;Item 3&lt;/asp:ListItem&gt;
                      &lt;/asp:DropDownList&gt;
  
                  &lt;/ContentTemplate&gt;
              &lt;/asp:UpdatePanel&gt;
  
          &lt;/div&gt;
  
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Default.aspx.cs</strong></p>
  
  <pre><code>using System;
  using System.Data;
  using System.Configuration;
  using System.Collections;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  
  namespace SessionTest
  {
      public partial class _Default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              // If I do this, the exception does not occur.
              if (Session["key"] == null)
                  Session.Add("key", 0);
          }
  
          protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
          {
              // If Session["key"] has not been created, setting it from
              // the async call causes the excaption
              Session.Add("key", ((DropDownList)sender).SelectedValue);
          }
      }
  }
  </code></pre>