Skip to main content
 首页 » 编程设计

c#之使用 Ajax 在 C# 中添加 session 变量

2024年01月28日34leader

我需要使用 ajax 将变量添加到 session 状态中,当我尝试这样做时它不起作用。谁能帮我解决这个问题。当我单击按钮时,它会重定向到 Travellerinfo.aspx 页面

下面是我的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 测试.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); 
 
        } 
 
 
    } 
} 

请您参考如下方法:

一定是

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

请注意:该方法必须是公共(public)的、静态的和EnableSession属性必须为真。

编辑 1:

添加'return 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); 
 
        }