How to control the Session using JavaScript

Source: Internet
Author: User

When you see this question, someone may ask, JavaScript represents the client, and Session represents the server (you don't know if you can understand it ).

First of all, I need to change the module code in the Session when I click a module during permission management. This operation is a tag, of course, I don't know if a linkbutton can jump to the page and modify the Session at the same time. I personally think it is okay. Can it be specific? If someone has done a Demo, I can leave a message to explain it.

The a tag is used to jump to the page. Its onclick event is also executed by the js method on this page. Now we return to the problem described in the question-write a JavaScript method, modify the Session.

In fact, this example is not difficult, but it is of extraordinary significance to me. This example relieves a majority of my fear of AJAX.
First, write a general processing program (that is, the server code)

It should be noted that if you want to modify the Session, you need to introduce the namespace and implement an interface (only implement 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 the 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?
Below 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 (){
// Determine whether the interaction with the server is complete, and whether the server returns data
If (xmlhttp. readyState = 4 ){
// Indicates that the interaction with the server is complete.
If (xmlhttp. status = 200 ){
// Alert ("data is returned correctly ");
Return;
}
}
}

In the callback function, I wrote only one statement for normal data returned by the test, and then commented out.
When binding events to tag a, I initially used the concatenation string method, that is, to find the module ID and module name from the database, and then concatenate the statement using the following statement:
Copy codeThe Code is as follows:
StringBuilder sbModel = new StringBuilder ();
// Concatenate menus 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 write errors like this. Although I wrote test statements before copying them, I then wrote the variables to the write location, however, the statements written in this way are difficult to debug.
In the process of communicating with others, I spoke about the Repeater control and suddenly realized that the Repeater control did not do the Repeater control? Why do we need to write such error-prone code on our own?
Copy codeThe Code is as follows:
<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>

Is it true that it is much clearer to write it like this? Because I used to splice strings and implement it. Using Repeater is just a thought. I don't know if it will be a problem if I splice IDs, if you have any questions, thank you.

So far, my first AJAX-related article has been completed. The understanding of AJAX is just the beginning, which does not involve data interaction, therefore, the explanation of some knowledge is still pale. With the deepening of learning, the use of AJAX will not just stay on the updatepanel and timer controls. For some instances that are not difficult, it is better to do it yourself.

For this example, you can ask why a LinkButton can be implemented. But I want to say that I didn't think of a LinkButton at first, the problem is complicated by yourself. This involves a page Jump. In fact, you can use LinkButton to solve the problem, but learning is a process, I used my own dumb to improve my understanding of AJAX. Why is it not a fast task ~~

The understanding of AJAX is still ongoing step by step. If there are any errors, I hope you will comment on them actively.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.