title: Adding Session Variable in C# using Ajax
body: <p>I need to add variable into session state using ajax , when I tries this It didn't work. can any one please help me on this. when I  click the Button It redirect to Travellerinfo.aspx page</p>
  
  <p>Below is my test.aspx </p>
  
  <pre><code>   &lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="hotelbeds.test" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; 
  &lt;!doctype html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;title&gt;jQuery UI Tooltip - Custom animation demo&lt;/title&gt;
  &lt;link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /&gt;
  &lt;script src="http://code.jquery.com/jquery-1.9.1.js"&gt;&lt;/script&gt;
  &lt;script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"&gt;&lt;/script&gt;
  &lt;link rel="stylesheet" href="/resources/demos/style.css" /&gt;
  &lt;script type="text/javascript"&gt;
      function testbook(hotelcode) {
  
          $.ajax({
              type: "POST",
              url: "test.aspx/addSession",
              data: "{'hotelcode':'" + hotelcode + "'}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (msg) {
                  window.location.href = "Hotelresul.aspx";
              },
              error: function (err) {
                  window.location.href = "TravellerInfo.aspx";
              }
          });
  
      }
  
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
  
      &lt;div&gt;
  
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>CS test.aspx</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Web.Services;
  using System.Configuration;
  using System.Drawing;
  
  namespace hotelbeds
  {
  
      public partial class test : System.Web.UI.Page
      {
  
          protected void Page_Load(object sender, EventArgs ea)
          {
              ImageButton testbtn = new ImageButton();
              testbtn.OnClientClick = "testbook('15000')";
              form1.Controls.Add(testbtn);
  
          }
          [WebMethod(EnableSession = true)]
          public static void addSession(String hotelcode)
          {
              HttpContext.Current.Session.Add("hotelcode", hotelcode);
  
          }
  
  
      }
  }
  </code></pre>