javascript - Adding Session Variable in C# using Ajax -
i need add variable session state using ajax , when tries didn't work. can 1 please me on this. when click button redirect travellerinfo.aspx page
below 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); } } }
it must be
[webmethod (enablesession=true)] public static void addsession(string hotelcode) { httpcontext.current.session.add("hotelcode", hotelcode); }
please note: method must public, static , enablesession attribute must true.
edit 1:
'return false' added prevent default function of button. default function of button post form server. alternatively, event.preventdefault() can used prevent 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); }
Comments
Post a Comment