Application tips: ASP and asp.net share session solutions

Source: Internet
Author: User
Tags foreach count sessions variable tostring
Asp.net|session| Skills | solve

In the original ASP program with ASP.net to add functional modules, shared sessions is a difficult point, the following describes a more concise method, you can share session sessions in ASP and ASP.net.

The login page is recreated using C #, executing the statement after a successful login:

Response.Write ("<script language= ' JavaScript ' >window.open (' aspxtoasp.aspx ', ' new ');</script>");

Open aspxtoasp.aspx page, action set to aspxtoasp.asp, you can post the session value to the ASP page, because it is one-way transmission, do not repeat the action on other pages, after testing, transmission success!

The aspxtoasp.aspx code is as follows:

<script language= "C #" runat= "Server" >
Response.Write ("<form name=t id=t action=aspxtoasp.asp method=post>");
foreach (Object it in session.contents)
{
Response.Write ("<input type=hidden name=" + it.) ToString ());
Response.Write ("value=" + session[it). ToString ()]. ToString () + ">");
}
Response.Write ("</FORM>");
Response.Write ("<script>t.submit ();</script>");
</script>

The aspxtoasp.asp code is as follows:

<%
For I=1 to Request.Form.Count
Session (Request.Form.Key (i)) =request.form (i)
Next
Response.End
%>
<script language= "JavaScript" >
Window.close ();
</script>

This is an old topic, because recently used, so there is no way to find information everywhere. Microsoft's Web site is provided by the database, always feel too cumbersome, but also said to use directly, but I did not try to succeed. I think it's impossible to use it directly. There is another way, is through a few page conversion, I use this method, by summing up the search to the data, sorted out after a few pages to convert. Mainly through the hidden input to achieve. The specific methods are as follows:

ASP goto asp.net page:

With an ASP page, the session information is written to the input, submitted to the ASP.net page

trans.asp

<%
"----------test Data--------
Session ("name") = "SRX"
Session ("id") = "1"
Session ("sex") = "F"
Session ("pass") = "Asdfas"
Session ("age") = "23"
Session ("weight") = "131"
’’--------------------------
Response.Write ("<form name=frm id=frm action=" "asptoaspx.aspx" "Method=post >")
For each Item in session.contents
Response.Write ("<input type=hidden name=" & Item)
Response.Write ("Value=" & Session (Item) & ">")
Next
If Len (Request.QueryString ("Destpage")) >4 Then
Response.Write ("<input Type=hidden name=destpage value=" & Request.QueryString ("Destpage") & ">")
End If
Response.Write ("</FORM>")
Response.Write ("<SCR" + "ipt>frm.submit (); </SCR" + "ipt>")
%>

Asptoaspx.aspx

<%@ Page language= "C #"%>
<script language=c# runat=server>
private void Page_Load (object sender, System.EventArgs e)
{
Session.Timeout = 60;
for (int i=0;i<request.form.count;i++)
{
Session[request.form.getkey (i)]=request.form[i]. ToString ();
}
Allsession (); Output all sessions, annotated when used
Try
{
if (session["Destpage"]. ToString (). Length >4)
{
Server.Transfer (session["Destpage"). ToString (), true);
}
}
Catch {}
}
private void Allsession ()
{
Response.Write ("There are" + Session.Contents.Count + "session <I>var</I>iables<P>");
foreach (Object obj in session.contents)
{
Response.Write ("session[" +obj). ToString () + "]-" +session[obj. ToString ()]. ToString () + "<br>");/output all sessions, annotated when used
}
}
</script>

asp.net to asp page:

With a ASP.net page, the session information is written to the input, submitted to the ASP page

Trans.aspx

<%@ Page language= "C #"%>
<script language=c# runat=server>
private void Page_Load (object sender, System.EventArgs e)
{
----------Test Data---------
session["name"] = "SRX";
session["Sex"]= "F";
//----------------------------
Response.Write ("<form name=frm id=frm action=aspxtoasp.asp method=post>");
foreach (Object obj in session.contents)
{
Response.Write ("<input type=hidden name=") +obj. ToString () + "'");
Response.Write ("value = '" +session[obj). ToString ()]. ToString () + "' >");
}
Try
{
if (request.querystring["Destpage"]. ToString (). Length > 4)
{
Response.Write ("<input type=hidden name= ' destpage '");
Response.Write ("value = '" +request.querystring["Destpage"). ToString () + "' >");
}
}
catch{}
Response.Write ("</form>");
Response.Write ("&LT;SCR" + "IPT language= ' JavaScript ' >frm.submit (); &LT;/SCR" + "ipt>");
}
</script>

aspxtoasp.asp

<%
For I=1 to Request.Form.Count
Session (Request.Form.Key (i)) =request.form (i)
Next
If Len (Session ("Destpage")) >4 Then
Response.Redirect (Session ("Destpage"))
End If
'----------output all the session-------------
Call Allsession () ' Use to comment out this line of code
function Allsession ()
Response.Write "There are" & Session.Contents.Count & "Session <I>var</I>iables<P>"
Dim StrName, Iloop
For each strName in session.contents ' use for Each loop to see Session.Contents
If IsArray (session (StrName)) Then ' if the session variable is an array? ' Circulate every element of a set of print
For iloop = LBound (Sessions (StrName)) to UBound (session (StrName))
Response.Write StrName & "(" & Iloop & ")-" & _
Session (StrName) (iloop) & "<BR>"
Next
Else ' in other cases, simply print the value of the variable
Response.Write StrName & "-" & Session.Contents (strName) & "<BR>"
End If
Next
End Function
’--------------------------------------------
%>



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.