C# 使用C中的代码隐藏文件将值从一种形式传递到另一种形式#
我试图检查从C# 使用C中的代码隐藏文件将值从一种形式传递到另一种形式#,c#,C#,我试图检查从default.aspx页面重定向到profile.aspx页面时是否设置了UserAuthInfo.Current的值。以下是我在这两页上的代码: public class MyGlobal { static string _variable; public static string Variable { get { return _variable; } set {
default.aspx
页面重定向到profile.aspx
页面时是否设置了UserAuthInfo.Current
的值。以下是我在这两页上的代码:
public class MyGlobal
{
static string _variable;
public static string Variable
{
get
{
return _variable;
}
set
{
_variable= value;
}
}
DEFAULT.ASPX代码段
if ((logonHolder.AccountStatus))
{
UserAuthInfo authInfo = new UserAuthInfo();
authInfo.UserID = logonHolder.UserID.ToString();
//strlogon_ID
authInfo.IsAuthenticated = true;
authInfo.Attributes.Add("logon_ID", logonHolder.UserID);
authInfo.Attributes.Add("Email", txtEmail.Text);
authInfo.Attributes.Add("UserType", logonHolder.UserType);
if (logonHolder.UserType == "A")
{
authInfo.Attributes.Add("IsAdmin", "Y");
}
else
{
authInfo.Attributes.Add("IsAdmin", "N");
}
authInfo.Roles.Add(logonHolder.UserType);
authInfo.Attributes.Add("LastName", logonHolder.LastName);
authInfo.Attributes.Add("FirstName", logonHolder.FirstName);
authInfo.Attributes.Add("user_employee_number", logonHolder.EmployeeID);
authInfo.Attributes.Add("user_id", logonHolder.UserID);
authInfo.Attributes.Add("SessionGUID", _holderService.CreateSession(strret_code, logonHolder.UserID));
authInfo.CreateTicket();
Session["Session"] = "Valid";
authInfo.Attributes.Add("user_login_security", LoginSecurity.Unrestricted);
**if (UserAuthInfo.Current != null && UserAuthInfo.Current.IsAuthenticated)
{
Response.Redirect("profile.aspx", false);
}**
if (UserAuthInfo.Current == null || !HttpContext.Current.User.Identity.IsAuthenticated)
{
Response.Redirect(FormsAuthentication.LoginUrl, true);
return;
}
Profile.aspx页面\加载代码段
if ((logonHolder.AccountStatus))
{
UserAuthInfo authInfo = new UserAuthInfo();
authInfo.UserID = logonHolder.UserID.ToString();
//strlogon_ID
authInfo.IsAuthenticated = true;
authInfo.Attributes.Add("logon_ID", logonHolder.UserID);
authInfo.Attributes.Add("Email", txtEmail.Text);
authInfo.Attributes.Add("UserType", logonHolder.UserType);
if (logonHolder.UserType == "A")
{
authInfo.Attributes.Add("IsAdmin", "Y");
}
else
{
authInfo.Attributes.Add("IsAdmin", "N");
}
authInfo.Roles.Add(logonHolder.UserType);
authInfo.Attributes.Add("LastName", logonHolder.LastName);
authInfo.Attributes.Add("FirstName", logonHolder.FirstName);
authInfo.Attributes.Add("user_employee_number", logonHolder.EmployeeID);
authInfo.Attributes.Add("user_id", logonHolder.UserID);
authInfo.Attributes.Add("SessionGUID", _holderService.CreateSession(strret_code, logonHolder.UserID));
authInfo.CreateTicket();
Session["Session"] = "Valid";
authInfo.Attributes.Add("user_login_security", LoginSecurity.Unrestricted);
**if (UserAuthInfo.Current != null && UserAuthInfo.Current.IsAuthenticated)
{
Response.Redirect("profile.aspx", false);
}**
if (UserAuthInfo.Current == null || !HttpContext.Current.User.Identity.IsAuthenticated)
{
Response.Redirect(FormsAuthentication.LoginUrl, true);
return;
}
运行代码时,在default.aspx页面上设置UserAuthInfo.Current的值,但在profile.aspx页面上该值始终为空。然后使用可用于所有页面的全局类,下面是我在两个页面上的代码:
public class MyGlobal
{
static string _variable;
public static string Variable
{
get
{
return _variable;
}
set
{
_variable= value;
}
}
并使用它:
MyGlobal.Variable = UserAuthInfo.Curren;
string current = MyGlobal.Variable ;
您可以使用页面的会话、查询字符串或公共属性将对象从一个页面发送到另一个页面。您在哪一行设置UserAuthInfo.Current?我看不到它…@OlafWatteroth op正在这里设置变量的实例`UserAuthInfo authInfo=new UserAuthInfo();`问题在于if语句应该检查
if(string.IsNullOrEmpty(authInfo)){}
而不是对象本身。这一行需要使用authInfo
变量if(UserAuthInfo.Current!=null&&UserAuthInfo.Current.IsAuthenticated)default.aspx页面中带有“if”的行仅用于执行检查。那条线没有停下来。但是,此时UserAuthInfo.Current正在设置。一旦我从default.aspx重定向到profile.aspx并尝试验证该值是否仍然设置,它将恢复为null…因此,当我从一个表单移动到下一个表单时,该值不会被保存。我确实在default.aspx表单中将ViewStateMode设置为Enabled。@MethodMan:创建本地实例不会设置(可能)静态变量UserAuthInfo.Current
@Graciascente:那么属性GET创建了一个实例?但为什么要将所有设置和数据设置为UserAuthInfo的本地实例?