正在服务器端变量(vbscript)中保存javascript变量
Saving javascript variable in server side variable (vbscript)
我知道你不能直接将javascript变量保存到服务器端变量(vbscript)中,但有没有办法解决这个问题,比如将java脚本变量保存到html隐藏输入中,然后使用javascript发布。这可能吗?如果没有,我还能做什么?下面是我的代码到目前为止得到的一个下拉列表的值-javascript
function selectedDatabase() {
select_temp = form1.elements["selection"];
select_index = select_temp.selectedIndex;
select_text = select_temp.options[select_index].text;
}
下面是HTML代码
<center><select id="selection" onchange="selectedDatabase()">
<option>Movies</option>
<option>Movies 2</option>
<option>New Movies</option>
<option>New Movies 2</option>
</select></center>
</td></tr>
您正在寻找的是ajax。您可以手动完成,也可以更好地使用JavaScript库,如MooTools、jQuery或Prototype。
查看谷歌大学的Ajax教程。我会避开w3schools的教程。
为了涵盖所有的基础,为什么不能让用户提交表格呢?
此外,您也可以使用cookie来实现这一点,尽管直到用户的下一次get或POST才会在服务器上获得cookie值。
可以将javascript变量值存储到服务器端变量中。您所要做的就是实现"System.Web.UI.ICallbackEventHandler"类。
下面是演示如何做到这一点的代码
- 在aspx页面中:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Client Calback Example</title>
<script type="text/ecmascript">
function LookUpStock()
{
var lb=document.getElementById("tbxPassword");
var product=lb.value;
CallServer(product,"");
}
function ReceiveServerData(rValue){
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="password" id="tbxPassword" />
<input type="Button" onclick="LookUpStock">Submit</button>
</div>
</form>
</body>
**
- 代码隐藏(CS)页
**
public partial class _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
protected String returnValue;
protected void Page_Load(object sender, EventArgs e)
{
String cbReference = Page.ClientScript.GetCallbackEventReference
(this,"arg", "ReceiveServerData", "context");
String callbackScript;
callbackScript = "function CallServer(arg, context)" +
"{ " + cbReference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
}
public void RaiseCallbackEvent(String eventArgument)
{
if(eventArgument == null)
{
returnValue = "-1";
}
else
{
returnValue = eventArgument;
}
}
public String GetCallbackResult()
{
return returnValue;
}
}
现在,您可以将JavaScript变量"product"值获取到服务器端变量"returnValue"中。
相关文章:
- 将JavaScript变量保存到Rails模型
- 将javascript变量保存到数据库中
- 将Javascript从PHP变量保存到文件
- 跨站点脚本过滤器阻止了我使用python-cgi脚本将javascript变量保存到mysqldb的尝试
- 需要将一个值从javascript变量保存到php会话
- Angular ui路由器,通过url/url变量保存状态
- 是否将Javascript变量保存为PHP变量?获取、发布、COOKIE
- 尝试将 PHP $_POST 变量保存到 javascript 中,但它不起作用
- 将 PHP 变量保存到函数中的脚本变量
- 如何让变量保存 mongodb 数据库
- 角度将变量保存到带有 ajax 调用的 laravel 会话不起作用
- 无法将变量保存到本地存储中
- 将 javascript 变量保存到 Ruby 中
- 如何在Symfony中将javascript变量保存到我的表单类型中
- 从javascript变量保存图像
- Backbone.js没有't将变量保存在集合中
- 将js函数中的一个变量保存到代码后面的server/#C中
- AngularJS是否将变量保存到文件中
- SAPUI5 jQuery.sap.storage变量保存
- 如何将Jmeter变量保存到csv文件