How JavaScript controls the implementation principle and code of session

Time:2021-10-17

Seeing this topic, some people may ask that JavaScript represents the client and session represents the server (I don’t know if you can understand this).

Let’s talk about the requirements first. When I do permission management, I need to change the module code in the session when I click a module, and an a tag is used to realize this operation. Of course, I don’t know whether a LinkButton can jump to the page and modify the session at the same time. I think it should be possible. Can I be specific, If someone makes a demo, you can leave a message.

The a tag implements the jump of the page. Its onclick event also executes the JS method in this page. Now we return 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 an unusual meaning for me. This example relieves most of my fear of Ajax.

First, write a general handler (that is, server 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 and do nothing else)

Copy codeThe code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace TGB.CJX
{
/// <summary>
///Modify 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;
}
}
}
}

Have you found that the functions implemented above are very simple?

Here is the client code

Copy codeThe 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 (“the current browser does not support creating XMLHttpRequest objects, please change the browser”);
return;
}

xmlhttp.onreadystatechange = callback;

xmlhttp.open(“GET”, “ModifySession.ashx?session=” + session, true);
xmlhttp.send(null);
}

function callback() {
//Judge whether the interaction with the server is completed and whether the server returns data
if (xmlhttp.readyState == 4) {
//Indicates that the interaction with the server is completed
if (xmlhttp.status == 200) {
//Alert (“data returned correctly”);
return;
}
}
}

In the callback function, I only wrote a statement to test the normal return data, and then commented it out.

When binding events for a tag, I initially used the method of splicing strings, that is, I looked up the module ID and module name from the database, and then spliced them through the following statement:

Copy codeThe code is as follows:
StringBuilder sbModel = new StringBuilder();
//Menu splicing of accessible modules
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>”);
}

However, it is easy to make mistakes. Although I wrote the test statement before copying it, and then wrote the variable to the write position, the statement written in this way is difficult to debug.

When talking about the repeater control in the process of communicating with others, I suddenly realized that the process I repeated is what the repeater control does? Control did it for us. Why write such error prone code yourself?

Copy codeThe code is as follows:
<asp:Repeater runat=”server”>
<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 I write it like this, it will be much clearer, because the splicing string I used before has been implemented. Using repeater is just an idea. I don’t know if there will be another problem in ID splicing. If there is a problem, please correct it.

So far, I have finished my first article on Ajax. The understanding of AJAX is just the beginning. There is no data interaction involved, so the explanation of some knowledge is still pale. With the deepening of learning, the use of Ajax will not only stay on the controls such as UpdatePanel and timer. For some examples that are not too difficult, it is better to do it yourself.

For this example, you can ask why it is so troublesome to use a LinkButton. But I want to say that I didn’t think that a LinkButton could solve the problem at the beginning. I complicated the problem by myself. This involves a page Jump. In fact, it can be solved by using a LinkButton, but learning is a process, It’s a pleasure to promote the understanding of Ajax with a little confusion~~

The understanding of AJAX is still going on step by step. If there are any mistakes, I hope you can make positive comments