我可以想到一些方法來做到這一點,但我會建議你這樣做。基本步驟如下:
使用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