我可以想到一些方法来做到这一点,但我会建议你这样做。基本步骤如下:
使用JavaScript弹出一个单独的模态对话框,其中包含要作为参数传递的全局变量(而不是查询字符串)。
在弹出页面中,使用JavaScript将全局变量设置为隐藏字段,然后启动对自身的回发。
在后面的代码中,从隐藏字段检索全局变量并将其存储在会话数据中。
现在,全局变量在会话空间中可用。
注意:您需要jQuery 1.7.1或更高版本。
第1步代码:
这是您的弹出式链接或按钮。
<a id="popAspxWeb" href="#" onclick="javascript:PopPage('ModalPopup.aspx','galbal_variable_data');">Click to Popup</a>
这里是JavaScript代码打开弹出窗口。
<script type="text/javascript">
function PopPage(page, data) {
var result = window.showModalDialog(
page, // popup this page
data, // with this data
"dialogWidth:500px; dialogHeight:500px; resizable:no; status:no; center:yes");
}
</script>
第2步代码:
你需要这些。第一个存储传入的数据,第二个标记页面回发或不存储。
<input id="hidData" runat="server" type="hidden" />
<input id="postback" runat="server" type="hidden" value="false" />
这里的JavaScript的:
<script type="text/javascript">
var data = ""; // global variable to store data from parent window.
$(document).ready(function() { //jQuery code to be executed when document is fully loaded.
args = window.dialogArguments; // obtain argument data and assign to global variable.
if ($("#postback").val().toString() == "false") { // if first visit to page
$("#hidData").val(data); // assign argument data from global variable to hidden field.
$("#postback").val("true"); // flag the form is posted back.
document.forms[0].submit(); // trigger form post.
}
});
function ReturnAndClose() { // you can optionally return data back to the parent window.
window.close(); // this closes modal pop up.
window.returnValue = document.getElementById('return_data').value; // this returns a value to parent window.
}
</script>
步骤3代码:
代码behine访问galbol变量数据。
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
string data = this.hidData.Value;
this.Session["data"] = data;
}
}
也可参考这个帖子http://stackoverflow.com/questions/1981673/persist-javascript-variables-across-pages – 2012-04-27 05:30:58
你怎么能在JavaScript设置会话?使用谷歌搜索,但似乎不可能 – user384080 2012-04-27 05:33:44
正如我前面提到的,你不能在JavaScript中设置会话。 – 2012-04-27 05:36:33