本文介绍了页面加载后的会话值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 亲爱的所有人, 我将一些价值从第一页传递到TheAdmin页面。 我可以在Session中分配值[ user]完美。但是当它重定向到那个页面空值传递时。请帮帮我。下面是我的代码。 1stPage UserInfo loggedin = new UserInfo(); loggedin.UserID = 1 ; loggedin.UserName = Manas; loggedin.SessionID = 111 ; loggedin.ClientName = Cl1; loggedin.ClientID = 1 ; 会话[ user] = loggidin; Response.Write( < script>); Response.Write( var height = screen.height;); Response.Write( var width = screen.width;); Response.Write( var params ='left = 0,top = 0,height ='+ height +',width ='+ width +',resizable = yes,scrollbars = yes,status = yes';); Response.Write( var win = window.open('TheoAdmin.aspx?rand = + new System.Random()。NextDouble()。ToString()+ ',' + objclientuser.UserName + _ Window + new System.Random()。Next()。ToString()+ ',params);); Response.Write( win.focus();); Response.Write( if(navigator.userAgent.indexOf('MSIE 6.0') != - 1){window.opener = window; window.close();}); Response.Write( else {window.open('','_ self',''); window.close()的;}); Response.Write( < / script>); 管理页面 <!DOCTYPE html> < script runat = 服务器 > string clientWidth = ; string clientHeight = ; string browser = ; string xmlString = ; 受保护 void Page_Load( object sender,EventArgs e) { UserInfo loggedin =(UserInfo)Session [ user]; if (loggedin == null ) { Response.Redirect( Logout.aspx); } else { clientWidth = 100%; clientHeight = 100%; xmlString + = < root>; xmlString + = < username> + loggedin.UserName + < / username>; xmlString + = < userid> + loggedin.UserID + < / userid>; xmlString + = < hosturl> + ConfigurationManager.AppSettings [ HostURL] + < / hosturl> 中; xmlString + = < serviceurl> + ConfigurationManager.AppSettings [ ServiceURL] + < / serviceURL中> 中; xmlString + = < random> + new System.Random()。NextDouble()。ToString()+ < / random> ;; xmlString + = < version> + ConfigurationManager.AppSettings [ TAVersion] + < /版本> 中; xmlString + = < sessionid> + loggedin.SessionID + < / sessionid>; xmlString + = < clientname> + loggedin.ClientName + < / clientname>; xmlString + = < subscriptionid> + HttpUtility.UrlEncode(loggedin.SubscriptionID) + < / subscriptionid>; // xmlString + =< auth> + Request.Cookies [LOOKINGGLASSFORMSAUTH]。值+< / auth>; xmlString + = < sess> + Request.Cookies [ ASP.NET_SessionId].Value + < / sess>; xmlString + = < currentyear> + System.DateTime.Now.Year。 ToString()+ < / currentyear>; xmlString + = < / root>; } } < / script > < html> 解决方案 我发现了一个拼写错误一瞥这里。 UserInfo loggedin = new UserInfo(); ................................. 会话[user] = loggidin; //它应该被登录 我不知道它是否可能是你问题的答案。如果不是问题,请告诉我。 问候......:笑: 你可以创建一个这样的类: 使用系统; 使用 System.Web; 命名空间 MyWeb { public class AppSession { public static UserInfo LoginUser { get { if (HttpContext.Current.Session [ userinfo] == null ) { HttpContext.Current.Session [ userinfo] = new UserInfo(); } return (UserInfo)HttpContext.Current.Session [ 用户信息]; } 设置 { // 检测空值 // 删除此IF ELSE检查是否要允许分配空值 if ( value == null ) { throw new 异常( 分配给Session-LoginUser的值。); } HttpContext.Current.Session [ userinfo ] = 值; } } } } 然后您可以在应用程序的任何位置访问它: public partial class WebForm1:System.Web.UI.Page { protected void Page_Load( object sender,EventArgs e) { if (!IsPostBack) { AppSession.LoginUser = new UserInfo(); AppSession.LoginUser.ClientID = 1 ; AppSession.LoginUser.SessionID = 1 ; AppSession.LoginUser.ClientName = Cl1; AppSession.LoginUser.UserID = 1 ; AppSession.LoginUser.UserName = Manas; } } } 然后在第二页: 受保护 void Page_Load( object sender,EventArgs e) { if (AppSession.LoginUser == null ) { Response.Redirect( 〜/ Logout。 ASPX); } } 现在,继续再试一次。只要为 AppSession.LoginUser 分配了空值,就会抛出异常。从那里你可以追踪你的代码中出现错误的地方。 那么这段代码: Response.Write( < script>); Response.Write( var height = screen.height;); Response.Write( var width = screen.width;); Response.Write( var params ='left = 0,top = 0,height ='+ height +',width ='+ width +',resizable = yes,scrollbars = yes,status = yes';); 可以写成这个: Response.Write( @ < script type =text / javascript> var height = screen.height; var width = screen.width; var params =' left = 0,top = 0,height ='+ height +',width ='+ width +',resizable = yes,scrollbars = yes,status = yes'; ); 您不必为每一行重复 Response.Write()。 如果你想逃避双引号符号 你可以逃避它像这样输入两次: 另一个例子使用 @: 此代码: xmlString + = < root>; xmlString + = < username> + loggedin.UserName + < / username>; xmlString + = < userid> + loggedin.UserID + < / userid>; xmlString + = < hosturl> + ConfigurationManager.AppSettings [ HostURL] + < / hosturl> 中; xmlString + = < serviceurl> + ConfigurationManager.AppSettings [ ServiceURL] + < / serviceURL中> 中; xmlString + = < random> + new System.Random()。NextDouble()。ToString()+ < / random> ;; xmlString + = < version> + ConfigurationManager.AppSettings [ TAVersion] + < /版本> 中; xmlString + = < sessionid> + loggedin.SessionID + < / sessionid>; xmlString + = < clientname> + loggedin.ClientName + < / clientname>; xmlString + = < subscriptionid> + HttpUtility.UrlEncode(loggedin.SubscriptionID) + < / subscriptionid>; // xmlString + =< auth> + Request.Cookies [LOOKINGGLASSFORMSAUTH]。值+< / auth>; xmlString + = < sess> + Request.Cookies [ ASP.NET_SessionId].Value + < / sess>; xmlString + = < currentyear> + System.DateTime.Now.Year。 ToString()+ < / currentyear>; xmlString + = < / root>; 可以写成: xmlString = @ < root> < username> + loggedin.UserName + @ < / username> < userid> + loggedin.UserID + @ < / userid> < hosturl> + ConfigurationManager.AppSettings [ HostURL] + @ < / hosturl> < ; serviceurl> + ConfigurationManager.AppSettings [ ServiceURL] + @ < / serviceurl> < random&g t; + new System.Random()。NextDouble()。ToString()+ @ < / random> < version> + ConfigurationManager.AppSettings [ TAVersion] + @ < / version> < sessionid> + loggedin.SessionID + @ < / sessionid> < clientname> + loggedin.ClientName + @ < / clientname> < subscriptionid> + HttpUtility.UrlEncode(loggedin.SubscriptionID)+ @ < ; / subscriptionid> < sess> + Request.Cookies [ ASP.NET_SessionId]。值+ @ < / sess> < currentyear> + System.DateTime.Now.Year.ToString()+ @ < / currentyear> < / root>; 您不必重复 xmlString + = 在每一行。 会话不需要我不知道这会对你有所帮助 string loggedin = convert.tostring(Session [ user]); if (loggedin == ) { Response.Redirect( Logout.aspx); } else { clientWidth = 100%; clientHeight = 100%; 如果loggdin为null然后你肯定会有另一个问题 reg ARDS ......:) Dear All,I am passing some value from 1st Page to TheAdmin Page.I can assign the value in Session["user"] perfectly .but when it redirects to that page null value passing.Please help me.Below is my code.1stPageUserInfo loggedin = new UserInfo();loggedin.UserID = 1;loggedin.UserName = "Manas";loggedin.SessionID = 111;loggedin.ClientName = "Cl1";loggedin.ClientID = 1;Session["user"]=loggidin; Response.Write("<script>");Response.Write("var height = screen.height;");Response.Write("var width = screen.width;");Response.Write("var params='left=0,top=0,height=' + height +',width=' + width + ',resizable=yes,scrollbars=yes,status=yes';");Response.Write("var win=window.open('TheoAdmin.aspx?rand=" + new System.Random().NextDouble().ToString() + "','" + objclientuser.UserName + "_Window" + new System.Random().Next().ToString() + "',params);");Response.Write("win.focus();");Response.Write("if(navigator.userAgent.indexOf('MSIE 6.0') !=-1){window.opener = window;window.close();}");Response.Write("else{window.open('', '_self', '');window.close();}");Response.Write("</script>");The Admin Page<!DOCTYPE html><script runat="server"> string clientWidth = ""; string clientHeight = ""; string browser = ""; string xmlString = ""; protected void Page_Load(object sender, EventArgs e) { UserInfo loggedin = (UserInfo)Session["user"]; if (loggedin == null) { Response.Redirect("Logout.aspx"); } else { clientWidth = "100%"; clientHeight = "100%"; xmlString += "<root>"; xmlString += "<username>" + loggedin.UserName + "</username>"; xmlString += "<userid>" + loggedin.UserID + "</userid>"; xmlString += "<hosturl>" + ConfigurationManager.AppSettings["HostURL"] + "</hosturl>"; xmlString += "<serviceurl>" + ConfigurationManager.AppSettings["ServiceURL"] + "</serviceurl>"; xmlString += "<random>" + new System.Random().NextDouble().ToString() + "</random>"; xmlString += "<version>" + ConfigurationManager.AppSettings["TAVersion"] + "</version>"; xmlString += "<sessionid>" + loggedin.SessionID + "</sessionid>"; xmlString += "<clientname>" + loggedin.ClientName + "</clientname>"; xmlString += "<subscriptionid>" + HttpUtility.UrlEncode(loggedin.SubscriptionID) + "</subscriptionid>"; //xmlString += "<auth>" + Request.Cookies["LOOKINGGLASSFORMSAUTH"].Value + "</auth>"; xmlString += "<sess>" + Request.Cookies["ASP.NET_SessionId"].Value + "</sess>"; xmlString += "<currentyear>" + System.DateTime.Now.Year.ToString() + "</currentyear>"; xmlString += "</root>"; } }</script><html> 解决方案 I found a typo at first glance here.UserInfo loggedin = new UserInfo();.................................Session["user"]=loggidin;//it should be loggedin I dont know whether it may be an answer to your question. Let me know if its not an issue.Regards..:laugh:You can create a class like this:using System;using System.Web;namespace MyWeb{ public class AppSession { public static UserInfo LoginUser { get { if (HttpContext.Current.Session["userinfo"] == null) { HttpContext.Current.Session["userinfo"] = new UserInfo(); } return (UserInfo)HttpContext.Current.Session["userinfo"]; } set { // Detect null value // Delete this IF ELSE checking if you want to allow a null value to be assigned if (value == null) { throw new Exception("Value assigned to Session-LoginUser detected."); } HttpContext.Current.Session["userinfo"] = value; } } }}Then you can access it any where in your application:public partial class WebForm1 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { AppSession.LoginUser = new UserInfo(); AppSession.LoginUser.ClientID = 1; AppSession.LoginUser.SessionID = 1; AppSession.LoginUser.ClientName = "Cl1"; AppSession.LoginUser.UserID = 1; AppSession.LoginUser.UserName = "Manas"; } }}Then at the second page:protected void Page_Load(object sender, EventArgs e){ if (AppSession.LoginUser == null) { Response.Redirect("~/Logout.aspx"); }}Now, go ahead and try again. Whenever there is a null value assigned to AppSession.LoginUser, an Exception will be thrown. From there you can trace where is the error occur in your code.By the way, this block of code:Response.Write("<script>");Response.Write("var height = screen.height;");Response.Write("var width = screen.width;");Response.Write("var params='left=0,top=0,height=' + height +',width=' + width + ',resizable=yes,scrollbars=yes,status=yes';");Can be written as this:Response.Write(@"<script type=""text/javascript""> var height = screen.height; var width = screen.width; var params='left=0,top=0,height=' + height +',width=' + width + ',resizable=yes,scrollbars=yes,status=yes';" );You won't have to repeat Response.Write() for each line.If you want to escape a double quote symbol "you can escape it by typing it twice like this: ""Another example of using @"":This code:xmlString += "<root>";xmlString += "<username>" + loggedin.UserName + "</username>";xmlString += "<userid>" + loggedin.UserID + "</userid>";xmlString += "<hosturl>" + ConfigurationManager.AppSettings["HostURL"] + "</hosturl>";xmlString += "<serviceurl>" + ConfigurationManager.AppSettings["ServiceURL"] + "</serviceurl>";xmlString += "<random>" + new System.Random().NextDouble().ToString() + "</random>";xmlString += "<version>" + ConfigurationManager.AppSettings["TAVersion"] + "</version>";xmlString += "<sessionid>" + loggedin.SessionID + "</sessionid>";xmlString += "<clientname>" + loggedin.ClientName + "</clientname>";xmlString += "<subscriptionid>" + HttpUtility.UrlEncode(loggedin.SubscriptionID) + "</subscriptionid>";//xmlString += "<auth>" + Request.Cookies["LOOKINGGLASSFORMSAUTH"].Value + "</auth>";xmlString += "<sess>" + Request.Cookies["ASP.NET_SessionId"].Value + "</sess>";xmlString += "<currentyear>" + System.DateTime.Now.Year.ToString() + "</currentyear>";xmlString += "</root>";Can be written as:xmlString = @"<root> <username>" + loggedin.UserName + @"</username> <userid>" + loggedin.UserID + @"</userid> <hosturl>" + ConfigurationManager.AppSettings["HostURL"] + @"</hosturl> <serviceurl>" + ConfigurationManager.AppSettings["ServiceURL"] + @"</serviceurl> <random>" + new System.Random().NextDouble().ToString() + @"</random> <version>" + ConfigurationManager.AppSettings["TAVersion"] + @"</version> <sessionid>" + loggedin.SessionID + @"</sessionid> <clientname>" + loggedin.ClientName + @"</clientname> <subscriptionid>" + HttpUtility.UrlEncode(loggedin.SubscriptionID) + @"</subscriptionid> <sess>" + Request.Cookies["ASP.NET_SessionId"].Value + @"</sess> <currentyear>" + System.DateTime.Now.Year.ToString() + @"</currentyear></root>";You won't have to repeat "xmlString +=" on each line.session does not required i dont know this will help you or not string loggedin = convert.tostring( Session["user"]);if (loggedin == ""){Response.Redirect("Logout.aspx");}else{clientWidth = "100%";clientHeight = "100%";if loggdin is null then you definately have another problemsregards......:) 这篇关于页面加载后的会话值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-21 11:11