__doPostBack(‘SetSessionPostBack‘, newValue);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input id="Button1" type="button" value="button" onclick="setSessionValue(‘hello‘);" /><div>
</div>
</form>
</body>
</html>
方法二:
使用AJAX
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
this.Session["SessionValue"] = "Original value";
}
this.Response.Write("SessionValue: " + this.Session["SessionValue"].ToString() + "<br>");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function makeAjaxCall(webUrl, queryString) {
var xmlHttpObject = null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttpObject = new XMLHttpRequest();
}
catch (ex) {
// Internet Explorer
try {
xmlHttpObject = new ActiveXObject(‘Msxml2.XMLHTTP‘);
}
catch (ex) {
xmlHttpObject = new ActiveXObject(‘Microsoft.XMLHTTP‘);
}
}
if (xmlHttpObject == null) {
window.alert(‘AJAX is not available in this browser‘);
return;
}
xmlHttpObject.open("GET", webUrl + queryString, false);
xmlHttpObject.send();
var xmlText = xmlHttpObject.responseText;
return xmlText;
}
function setSessionValue(newValue) {
var webUrl = ‘AjaxPage.aspx‘;
var queryString = ‘?SessionValue=‘ + newValue;
var returnCode = makeAjaxCall(webUrl, queryString);
//alert(returnCode);
<%= ClientScript.GetPostBackEventReference(this, string.Empty) %>;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input id="Button1" type="button" value="button" onclick="setSessionValue(‘Lance Zhang‘);" /><div>
</div>
</form>
</body>
</html>