2013-08-16 16 views
1

我需要使用ajax將變量添加到會話狀態,當我嘗試這樣做時,它不起作用。任何人都可以幫助我。當我點擊按鈕將其重定向到Travellerinfo.aspx頁使用Ajax在C#中添加會話變量

下面是我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); 

     } 


    } 
} 
+0

你應該調試到'addSession'方法(設置斷點)並檢查它是否被執行。並使其成爲「公共」訪問者。 – thmshd

+1

您可以將WebMethod保存在.aspx頁面中。但該方法必須是公開的和靜態的。看到我的答案。 @RGraham –

回答

6

它必須是

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

     } 

請注意:該方法必須是公共的,靜態的和EnableSession屬性必須爲真。

EDIT 1:

'返回false' 加到防止按鈕的默認功能。按鈕的默認功能是將表單發佈到服務器。或者,event.preventDefault()可用於防止默認功能。

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

編輯2:

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

     } 
+0

我嘗試了你的代碼,但仍然無法正常工作。 –

+0

請從瀏覽器(chrome)按ctrl + shift + J並選擇控制檯選項卡。在執行(即點擊按鈕)代碼後,請報告錯誤。 –

+0

請看更新的回答 –

-1

你不需要既不阿賈克斯也不會話變量來做到這一點。只需在酒店代碼中添加隱藏字段值並在提交時使用它。

希望答案很清楚。