1

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

Below is my test.aspx

   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="hotelbeds.test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Tooltip - Custom animation demo</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script type="text/javascript">
    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";
            }
        });

    }

</script>
</head>
<body>
    <form id="form1" runat="server">

    <div>

    </div>
    </form>
</body>

</html>

CS test.aspx

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);

        }


    }
}
2
  • You should debug into the addSession method (set breakpoint) and check if it's excuted. And make it a public accessor. Commented Aug 16, 2013 at 7:04
  • 1
    You can keep WebMethod inside an .aspx page. But the method must be public and static. See my answer. @RGraham Commented Aug 16, 2013 at 7:06

1 Answer 1

8

It must be

[WebMethod (EnableSession=true)]
        public static void addSession(String hotelcode)
        {
            HttpContext.Current.Session.Add("hotelcode", hotelcode);

        }

Please Note: The method must be public, static and EnableSession attribute must be true.

EDIT 1:

'return false' is added to prevent the Default function of a button. The default function of a button is to post form to server. Alternatively, event.preventDefault() can be used to prevent the default functionality.

<script type="text/javascript">
    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";
            }
        });


 return false;

    }

</script>

EDIT 2:

 protected void Page_Load(object sender, EventArgs ea)
        {
            ImageButton testbtn = new ImageButton();
            testbtn.OnClientClick = "return testbook('15000')";
            form1.Controls.Add(testbtn);

        }
Sign up to request clarification or add additional context in comments.

5 Comments

please press ctrl+shift+J from your browser (chrome) and chose console tab. After executing (ie clicking the button) the code please Quote the error reported.
I did try that also, still no luck
Run your Project (press F5). After executing this ajax call, from visual studio > debug> view intelli trace events> expand the last exception caught and see the error.
added 'return' to onClientClick and returned false to your javaScript function. This should work. please check the updated answer.
You are welcome ! 'return false' is added to prevent the Default function of a button. The default function of a button is to post form to server. Alternatively, event.preventDefault() can be used to prevent the default functionality. api.jquery.com/event.preventDefault

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.