如何从javascript调用会话,这是使用c#的代码隐藏创建的
本文关键字:代码 创建 隐藏 javascript 调用 会话 | 更新日期: 2023-09-27 18:13:14
大家好,我已经在c#中创建了会话,我必须在Javascript中使用它,它正在得到错误,它没有带来正确的会话,请帮助我解决这个问题
Javascriptvar terminalid = '<%= Session["TerminalID"] %>';
var hospid = '<%= Session["Hospitalid"] %>';
var terminalid = '<%= Session["TerminalID"] %>';
var userid = '<%= Session["UserID"] %>';
if (name != '') {
$.ajax({
type: "Post",
contentType: "application/json; charset=utf-8",
url: "E_Locations.aspx/AddNewLoc",
data: "{'locname':'" + name + "','hid':'"+hospid+"','tid':'"+terminalid+"','uid':'"+userid+"'}",
dataType: "json",
success: function (data) {
}
c#代码
Session["TerminalID"] = 180;
[WebMethod(EnableSession=true)]
public static string AddNewLoc(string locname,int hid,int tid,int uid)
{
string msg = string.Empty;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("EXECUTE S_BMD_SAVE_Location @FLAG=100, @PK_BioLocationID=0, @LocationNAME='" + locname + "', @HOSPID='" + hid + "', @TERMINALID='"+tid+"', @OPERATORID='"+uid+"'", con))
{
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i == 1)
{
msg = "true";
}
else
{
msg = "false";
}
}
}
return msg;
}
这可能会帮助你…
WebForm1.aspx
<script src="../js/jquery.js" type="text/javascript"></script>
<script>
$(function () {
var terminalid = '1';
var hospid = '2';
var userid = '4';
var obj = {};
obj.terminalid = terminalid;
obj.hospid = hospid;
obj.userid = userid;
$.ajax({
type: "POST",
url: "WebForm1.aspx/AddNewLoc",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
console.log(result.d);
}
});
});
</script>
WebForm1.aspx.cs
[WebMethod]
public static string AddNewLoc(string terminalid, string hospid, string userid)
{
//access session like this from backend or set sesssion value
long TerminalID = Convert.ToInt64(HttpContext.Current.Session["TerminalID"]);
//Do some stuf
return "true";
}
try this
string jsFunc = "alert(" + Session["TerminalID"] + ")";
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myJsFn", jsFunc , true);