问题
I am trying to pass a vbscript session variable to my c# page. Upon research I found this page:
Transfer Session Variables from Classic ASP to ASP.NET
I don't know how to change my
Int32 intresortID = Convert.ToInt32(Request.QueryString["TypeID"]);
to read in the session variable in the same manner as from the code provided in the link above(pasted below:)
<TITLE>ASPNETPage1.aspx</TITLE>
<%@ Page language="c#" %>
<script runat=server>
// We iterate through the Form collection and assign the names and values
// to ASP.NET session variables! We have another Session Variable, "DestPage"
// that tells us where to go after taking care of our business...
private void Page_Load(object sender, System.EventArgs e)
{
for(int i=0;i<Request.Form.Count;i++)
{
Session[Request.Form.GetKey(i)]=Request.Form[i].ToString();
}
Server.Transfer(Session["DestPage"].ToString(),true);
}
</script>
==============================================================================
<TITLE>FinalPage.aspx</TITLE>
<%@ Page language="c#" %>
<script runat=server>
// This page is just a "proof of concept page"...
private void Page_Load(object sender, System.EventArgs e)
{
Response.Write("Shared Session Variable Names/Values between Classic ASP and ASP.NET:<BR>");
for (int i = 0; i < Session.Contents.Count; i++)
{
Response.Write("Assigned to \"" +Session.Keys[i].ToString()+"\"");
Response.Write(" Value: "+ Session[i].ToString() +"<BR>");
}
}
</script>
Update: Code for where session variable is called on C# page:
private void FillGrid()
{
string connStr = ConfigurationManager.ConnectionStrings["bdsConnectionString"].ConnectionString;
using (SqlConnection Con = new SqlConnection(connStr))
{
Con.Open();
Int32 intresortID = Convert.ToInt32(Request.Form["TypeID"]);
Label4.Text = Convert.ToString(intresortID);
Label4.Visible = true;
DateTime startdate;
startdate = Convert.ToDateTime(TextBox1.Text);
Int32 ed = Convert.ToInt32(TextBox2.Text);
DateTime enddate;
enddate = startdate.AddDays(ed);
string str = "SELECT TOP (100) PERCENT tblAvail.dtm as Dtm, tblResortsRooms.strRoomType as strRoomType, tblResortsRooms.strDescription as strDescription, tblAvail.intQty as intQty, tblAvail.curPrice as curPrice, tblResortsRooms.intWSCode as intWSCode FROM tblAvail INNER JOIN tblResortsRooms ON tblAvail.intResortID = tblResortsRooms.intResortID AND tblAvail.strRoomType = tblResortsRooms.strRoomType WHERE (tblResortsRooms.curRecRate > 0) AND (tblAvail.intResortID = @intResortID) and (tblAvail.dtm between @startdate and @enddate) ORDER BY tblResortsRooms.strRoomType";
SqlDataAdapter sdr = new SqlDataAdapter(str, Con);
sdr.SelectCommand.Parameters.AddWithValue("@intResortID", intresortID);
sdr.SelectCommand.Parameters.AddWithValue("@startdate", startdate);
sdr.SelectCommand.Parameters.AddWithValue("@enddate", enddate);
DataTable dt = new DataTable();
sdr.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
Button2.Visible = true;
}
}
}
Can somebody please show me how?
回答1:
Bottom line is that a session variable in Classical ASP is not available to ASP.Net (regardless of the language used). You will need to make that value available to the ASP.Net page via a query string or form that gets posted back to the ASP.Net page. So a suggested solution is to pass the value (which apparently is in a session variable in the classical ASP page) via a query string to the ASP.Net page. Then on the ASP.Net page retrieve the query string value and do whatever you want with it.
I hope I haven't missed the point. But bottom line is there is no interaction between session variables in Classical ASP and ASP.Net because they use completely different frameworks and engines.
回答2:
I would recommend to follow Microsoft path http://msdn.microsoft.com/en-us/library/aa479313.aspx
回答3:
First off, in "ASPNETPage1.aspx" you are copying the whole Form
collection to Session
variables - which is absolutely unnecessary, since you are already preserving it with the Server.Transfer
: http://msdn.microsoft.com/en-us/library/caxa892w.aspx
Secondly, in "FinalPage.aspx", instead of iterating through the whole Session
state, you can access that same Form
collection directly through the Page.PreviousPage
property: http://msdn.microsoft.com/en-us/library/system.web.ui.page.previouspage.aspx
UPDATE: wow, my bad there, I really thought you were talking about Visual Basic
(as your question title, article link and sample code would all suggest) and not VBScript
- which is a client language, so you would need an equivalent of JavaScript PageMethod
or a WebService
(never used VBScript
, so don't know if it supports those things) to access the Session
state on the server
my corrections for your code sample still stand, offcourse
UPDATE 2: PageMethod
or a WebService
is implemented by the server language (Visual Basic
or C#
) on the server, and VBScript
or JavaScript
(both client languages) are just used to access it from the client - you need to learn to differentiate between server and client languages...
So, for the final time, are you using Visual Basic
or VBScript
? And post your original code if you expect any meaningful help from others.
回答4:
No experience with C# but based on my classic ASP experience id should be
Int32 intresortID = Convert.ToInt32(Request.Form[i]);
or if you know the name of the key, TypeID in this case, no need for the For loop
Int32 intresortID = Convert.ToInt32(Request.Form("TypeID"));
If this doens't work, try splitting up and logging
for(int i=0;i<Request.Form.Count;i++)
{
loggingRoutine(Request.Form.GetKey(i) + "=" + Request.Form[i].ToString());
}
(as i said, i'm no C# guy, just to get you an idea)
The Sessionvariables are only to remember values between page refreshes and between other pages, if you read the form directly in the first .ASPX there is no need for them.
来源:https://stackoverflow.com/questions/10679572/transfer-session-variables