I have a web page that I need to have global variables that are set each time a page is visited. The variables store information on whether a user is logged on or not. What I have done is created a user control that contains shared strings that are the global variables. I called the class "Login" for this control. The code for the control is like below.
------------
Public Class Login
Inherits System.Web.UI.UserControl
Public Shared UserID as String = "-1"
Public Shared UserName as String = ""
Public Shared UserLogged as String = "0"
Public Shared UserLevel as String = "0"
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
if Not Request.Cookies("dmginfo") Is Nothing then
UserID = Server.HtmlEncode(Request.Cookies("dmginfo")("ID"))
UserName = Server.HtmlEncode(Request.Cookies("dmginfo")("username"))
UserLogged = Server.HtmlEncode(Request.Cookies("dmginfo")("logged"))
UserLevel = Server.HtmlEncode(Request.Cookies("dmginfo")("level"))
Dim aCookie As New HttpCookie("dmginfo")
aCookie.Values("ID") = UserID
aCookie.Values("username") = UserName
aCookie.Values("logged") = UserLogged
aCookie.Values("level") = UserLevel
aCookie.Expires = DateTime.Now.AddDays(8)
Response.Cookies.Add(aCookie)
else
UserID = "-1"
UserName = ""
UserLogged = "0"
UserLevel = "0"
end if
End Sub
End Class
------------
Now, I put <%@dotnet.itags.org. Register TagPrefix="DMG" TagName="Login" src="http://pics.10026.com/?src=inc_login.ascx" %> at the top of my aspx page and included a <DMG:Login runat="server" /> tag in the markup and it includes the control just fine. In the code for the .aspx page I will call those variables by typing "Login.UserID", "Login.UserLogged", etc. This works fine until someone else visits the page from another computer. As they hit the page the global variables are still the same as the previous person left them. So if "georgie" is logged in and Login.UserName = "georgie", then "mike" visits the page he will see "welcome georgie" the first time. Then if mike refreshes, the variables are reset.
Why is it taking one view of the page to reset these values instead of doing it the first time? And is there a better way to do global variables?
It is better to use Session(for single user) or Application(for whole application) objects
Session("UserID") = "-1"
and so on
regards
HTH
Thanks for all of the replies today. You have helped a lot.
I am switching everything over to the session method, but I have one last hangup. If I declare say "Session("UserID") = AValue" in my user control's class, it still takes one load of the main page with that control in it before the session value gets set.
So if I set the session variable in inc_login.ascx and then import that into default.aspx, the first time I run the page anywhere I use Session("UserID") comes back with a NULL error.
Am I going to need to move all of my session declarations to the main default.aspx code-behind and do this for every single page in order to have those values set immediately the first time a user hits the site and then be available on that very same page?
Set your session variables in the global.asax file, in the Session_Start method; that's the place for session initialisation code.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconGlobalasaxSyntax.asp
A nice trick if you are using authentification is to store your "session" variables in a database keyed on the users logon, and resinstate their "session" whenever they log on. I use this technique to store each of my applications users preferences (colours, fonts, sizes, 2d/3d graphs etc) and the values of any key data fields they last referenced so they can always pick upwhat they were doing last time, even if they access the app via a different PC. I'll leave you to work out how to save the users settings if and when they change!
0 comments:
Post a Comment