2013-08-16 2 views
1

Ajax를 사용하여 세션 상태에 변수를 추가해야합니다. 시도 할 때 작동하지 않았습니다. 어느 누구도이 일을 도와 줄 수는 없어요. 나는 버튼을 클릭 할 때 페이지를 Travellerinfo.aspx로 리디렉션C#에서 세션 변수 추가하기 Ajax를 사용하여

다음

그것은해야합니다

<%@ 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가

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' 메소드 (breakpoint 설정)로 디버깅하여 실행 여부를 확인해야합니다. 그리고 그것을'public' 접근 자로 만드십시오. – thmshd

+1

WebMethod를 .aspx 페이지 안에 보관할 수 있습니다. 그러나이 방법은 공개적이고 정적이어야합니다. 내 대답을 보라. @RGraham –

답변

6

을에서 Test.aspx 내에서 Test.aspx입니다

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

     } 

참고 사항 : 메소드는 public, static이어야하며 EnableSession 속성이 true이어야합니다.

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

브라우저 (크롬)에서 ctrl + shift + J를 누르고 콘솔 탭을 선택하십시오. 코드를 실행 (즉, 버튼 클릭) 한 후 오류를 알려주십시오. –

+0

업데이트 된 답변을 참조하십시오 –

-1

이 작업을 수행하기 위해도 아약스이나 세션 변수가 필요하지 않습니다. 호텔 코드에 숨겨진 필드 값을 추가하고 제출시 사용하십시오.

답변이 명확하기를 바랍니다.

관련 문제