gpt4 book ai didi

javascript - 在javascript函数中如何修改 session 变量

转载 作者:行者123 更新时间:2023-11-30 13:06:20 24 4
gpt4 key购买 nike

我有以下代码。

在asp.net中,我设置了session变量,然后传给javascript进行修改。在 javascript 中,我可以读取 session 变量值并在 TextBox1 中返回修改后的值。再次在 asp.net 中,我收到修改后的 session 变量值并将其存储在 session 变量中。

protected void Page_Load(object sender, EventArgs e)
{
Session["MyTest"] = "abcd";

String csname = "OnSubmitScript";
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the OnSubmit statement is already registered.
if (!cs.IsOnSubmitStatementRegistered(cstype, csname))
{
string cstext = " document.getElementById(\"TextBox1\").value = getMyvalSession() ; ";
cs.RegisterOnSubmitStatement(cstype, csname, cstext);
}

if (TextBox1.Text.Equals("")) { }
else {
Session["MyTest"] = TextBox1.Text;
}

}


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<script language=javascript type="text/javascript">
function getMyvalSession() {

var txt = "efgh";
var ff = '<%=Session["MyTest"] %>' + txt;
return ff ;
}
</script>

</head>
<body>

<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack=true ></asp:TextBox>
<input type="submit" value="Submit" />
</div>
</form>
</body>
</html>

但我的目标是—— 在 javascript 函数本身中,我应该能够修改 session 变量。而且我不想使用提交按钮。

最佳答案

使用 cookie 我可以保存值(value)。提交按钮也不是必需的。所以这段代码已经解决了我的目的。

 <script language="javascript" type="text/javascript">

function writeCookie(name,value,days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires=" + date.toGMTString();
}else{
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}


function readCookie(name) {
var i, c, ca, nameEQ = name + "=";
ca = document.cookie.split(';');
for (i = 0; i < ca.length; i++)
{
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return '';
}


function restore(){
var sId = readCookie('sessionId');
document.getElementById("TextBox1").value = sId ;
}


function backup() {

var sId = document.getElementById("TextBox1").value;
writeCookie('sessionId', sId, 3);
}


function getMyvalSession() {
var ff = "Loading Value";
return ff;
}


function TextBox1_TextChanged() {
backup();
}


</script>








<body onload="restore()">

<form id="form1" runat="server">
<div>

<asp:TextBox ID="TextBox1" Name="TextBox1" runat="server"
AutoPostBack="True" onchange="TextBox1_TextChanged()" ></asp:TextBox>

</div>
</form>
</body>


protected void Page_Load(object sender, EventArgs e)
{
Loading();
}


void Loading (){

String csname = "OnSubmitScript";
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the OnSubmit statement is already registered.
if (!cs.IsOnSubmitStatementRegistered(cstype, csname))
{
string cstext = " document.getElementById(\"TextBox1\").value = getMyvalSession() ; ";
cs.RegisterOnSubmitStatement(cstype, csname, cstext);
}


}

关于javascript - 在javascript函数中如何修改 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15590634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com