This article will introduce to you how to control Session operations using JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Seeing this question, some people may raise questions. JavaScript represents the client, while Session represents the server (I don’t know if everyone can understand this).
Let me talk about the requirements first. When I am doing permission management, I need to change the module code in the Session accordingly when I click on a certain module. What implements this operation is an a tag. Of course I don’t know. Is it possible for a link button to jump to the page and modify the session at the same time? Personally, I think it is possible. If someone has made a demo, you can leave a message to explain.
a tag implements page jump. Its onclick event also executes the js method in this page. Now we are back to the problem described in the title - write a JavaScript method to modify the Session. .
In fact, this example is not difficult, but it has extraordinary personal significance to me. This example relieved a large part of my fear of AJAX.
First, write a general handler (that is, server-side code)
It should be noted that if you want to modify the Session, you need to introduce an additional namespace and implement an interface (you only need to implement it, otherwise No need to do anything)
The code is as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.SessionState; namespace TGB.CJX { /// <summary> /// 修改Session /// </summary> public class ModifySession : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Session["modelID"]=context.Request.QueryString["session"].ToString(); //context.Session["modelID"] = "1"; //context.Response.Write(context.Session["modelID"]); } public bool IsReusable { get { return false; } } } }
Did you find that the function implemented above is very simple?
The following is the client code
The code is as follows:
var xmlhttp; var session; function submit(obj) { //session = obj.id.substr(1, 1); session = obj.id.replace("model",""); //IE7,IE8,FF,MOZILLA,SAFARI if (window.XMLHttpRequest) { //alert("IE7,IE8,FF,MOZILLA,SAFARI"); xmlhttp = new XMLHttpRequest(); if (xmlhttp.overrideMinmeType) { xmlhttp.overrideMinmeType("text/xml"); } } else if (window.ActiveXObject) { //alert("IE5,IE6"); var activeName = ["MSXML2.XMLHTTP", "Miscrosoft.XMLHTTP"]; for (var i = 0; i < activeName.length; i++) { try { xmlhttp = new ActiveXObject(activeName[i]); break; } catch (e) { return; } } } if (xmlhttp == undefined || xmlhttp == null) { alert("当前浏览器不支持创建XMLHTTPREQUEST对象,请更换浏览器"); return; } xmlhttp.onreadystatechange = callback; xmlhttp.open("GET", "ModifySession.ashx?session=" + session, true); xmlhttp.send(null); } function callback() { //判断和服务器的交互是否完成,还要判断服务器端是否返回了数据 if (xmlhttp.readyState == 4) { //表示和服务器端的交互完成 if (xmlhttp.status == 200) { //alert("正确返回了数据"); return; } } }
In the callback function, I only wrote a statement to test the normal return of data, It was later commented out.
When binding events to the a tag, I initially used the method of splicing strings, which is to find the module ID and module name from the database, and then splice the statements through the following statement:
StringBuilder sbModel = new StringBuilder(); //将可以访问的模块进行菜单拼接 for (int i = 0; i < dtModel.Rows.Count; i++) { sbModel.Append("<li><a id='model" + dtModel.Rows[0]["mdlID"].ToString() + "' href='SpaceWeb.aspx' target='_parent' runat='server' onclick='submit(this)'>" + dtModel.Rows[i]["mdlName"].ToString() + "</a></li>"); }
But writing like this is easy to make mistakes. Although I wrote test statements before copying in, and then wrote the variables to the writing position, the statements written in this way are difficult to debug.
In the process of communicating with others, I talked about the Repeater control, and suddenly realized that the process I repeated was what the Repeater control did? The control does it for us, why do we need to write such error-prone code ourselves?
<asp:Repeater runat="server" id="rptModel"> <ItemTemplate> <li><a id='model'+'<%#Eval("mdlID")%>' href="SpaceWeb.aspx" target="_parent" runat="server" onclick="submit(this)"><%#Eval("mdlName" %></a></li> </ItemTemplate> </asp:Repeater>
If you write it like this, it will feel much clearer, because I used to splice strings before and implemented it. Using Repeater is just an idea. I don’t know if there will be any problems when splicing ids. , if there is any problem, please correct me.
So far, I have finished writing my first article about AJAX. The understanding of AJAX is just the beginning. It does not involve data interaction, so the explanation of some knowledge is still a bit pale. With the deepening of learning, the use of AJAX will not just stop at controls such as updatepanel and timer. For some examples that are not too difficult, it is better to do it yourself.
Regarding this example, you can ask why it’s so troublesome when you can actually use a LinkButton to achieve it. But I want to say that I didn’t think that a LinkButton could solve the problem at first. I just complicated the problem myself. Yes, this involves a page jump. In fact, it can be solved with LinkButton, but learning is a process. It is a pleasure to use a little confusion I made to promote my understanding of AJAX~~
The understanding of AJAX is still being progressed step by step. If there are any mistakes, please leave comments and comments
For more programming-related knowledge, please visit: Introduction to Programming! !