|
- 用JavaScript写Session的两种方法
- 方法一:
- 使用postback
- 复制代码
- <%@ 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)
- {
- // Insure that the __doPostBack() JavaScript method is created
- this.ClientScript.GetPostBackEventReference(this, string.Empty);
- if (this.IsPostBack)
- {
- string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
- string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
- if (eventTarget == "SetSessionPostBack")
- this.Session["SessionValue"] = eventArgument;
- }
- else
- {
- 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 setSessionValue(newValue) {
- __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>
- 复制代码
-
- 方法二中,设置session需要一个额外的aspx页面:
-
- 复制代码
- <%@ 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)
- {
- string sessionValue = (this.Request["SessionValue"] == null) ? string.Empty : this.Request["SessionValue"];
- string returnValue = "Sesson value changed to " + sessionValue;
- this.Session["SessionValue"] = sessionValue;
- this.Response.ClearHeaders();
- this.Response.Clear();
- this.Response.Write(returnValue);
- this.Response.End();
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>
- 复制代码
复制代码
|
|