Reputation: 6955
I'm quite new in the wonderful world of C# and I'm not sure how to cope with such a problem.
I have a .NET website on Visual Studio 2008, and I have a main page, main.aspx
(with its following main.aspx.cs
) and a game page, game.aspx
(also with game.aspx.cs
).
In the main class in main.aspx.cs
I have a static variable, private static string _username;
and with it, a method called getUsername that simply returns this value.
Now in my game class (game.aspx.cs
) I have an event handler, saveButton_Click()
, and in this function I need the value of _username
!
If it helps, a user starts on the main page, and then logs in... the variable _username
is saved if the login is valid and then the user clicks a link to go to the game.aspx
page!
I've looked into the internal keyword, but I am not sure how to use it in this particular example or even if it's the right thing to do in this example!
Upvotes: 0
Views: 1732
Reputation: 1961
IMHO, you could use Forms Based Authentication which would then make it very simple to get the user name, via User.Identity.Name
.
Upvotes: 0
Reputation: 52241
You have to use Sessions
to store and access the value in multiple pages:
//To store value in session variable
Session["UserName"] = "UserName";
//To access the value in the session variable
String UserName = Session["UserName"];
Check for Details ASP.NET Session State
Upvotes: 1
Reputation: 7013
Add this to your main page and game page classes:
protected string UserName {
get {
string retVal=string.Empty;
if (!string.IsNullOrEmpty(Session["UserName"]))
retVal=Session["Username"].ToString();
return retVal;
}
set{
Session["UserName"]=value;
}
}
and then access the property from your code;
Upvotes: 3
Reputation: 887245
To answer your question, you can make a public static property in a separate class.
However, don't.
This model will fail horribly (or worse) when two people use your site at once.
Instead, you should use Session State.
Upvotes: 4