you have no another solution but you still have an optional methods so as not to add the code to each page
1-you can define a webuser control that do the code (if session...........) and drag and drop it in each page (this control will be invisible thus has no effect in UI)
2-you can construct a class that takes in its constructor a webpage as a parameter and check page.session[userID] if null redirect then in each page just call at page load the constructor of that class providing "this" as the parameter
or else if u want to wait till session expires which i guess not your case you can handle this in global.asax
hope being usefull , feel free for futher questions
devangopinat...
Member
28 Points
76 Posts
how to check Session null or not ?
Jun 27, 2008 11:03 AM|LINK
hi to all
i need guidelines to check Session null or not in all page. but i don't want add code to all page.below code
Private void Page_Load()
{
if(Session["UserID"]==null)
{
Resoponse.Redirect("login.aspx");
}
}
i don't need type all page above code. commonly check Session null or not. please guide me.
by
Devan.G
PassHours
Contributor
4322 Points
736 Posts
Re: how to check Session null or not ?
Jun 27, 2008 11:14 AM|LINK
Check this : Managing Session variables
Hope that helps [Yes]
We make a living by what we get, but we make a life by what we give.
::: Winston Churchill :::
mohamed.alsa...
Participant
866 Points
134 Posts
Re: how to check Session null or not ?
Jun 27, 2008 11:19 AM|LINK
Hi Try this,
if(Session.Count > 0) === This line gives Whether the Session is live or not.
Go thru this URL for further discussion
http://aspalliance.com/810_Implementing_a_Singleton_Pattern_in_C
Hope this helps.
IF THIS POST HELPS YOU, CLICK MARK AS ANSWER
SaloZaSolo
Member
274 Points
79 Posts
Re: how to check Session null or not ?
Jun 27, 2008 11:22 AM|LINK
Greeting there,
you have no another solution but you still have an optional methods so as not to add the code to each page
1-you can define a webuser control that do the code (if session...........) and drag and drop it in each page (this control will be invisible thus has no effect in UI)
2-you can construct a class that takes in its constructor a webpage as a parameter and check page.session[userID] if null redirect then in each page just call at page load the constructor of that class providing "this" as the parameter
or else if u want to wait till session expires which i guess not your case you can handle this in global.asax
hope being usefull , feel free for futher questions
http://www.dsoftworld.com
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: how to check Session null or not ?
Jun 27, 2008 11:23 AM|LINK
Create a basePage like this
public class BasePage :System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (HttpContext.Current.Session["UserID"] == null) HttpContext.Current.Response.Redirect("login.aspx"); } }Then change your pages to inhertis from it instead of Inheriting from System.Web.UI.Page
hjsoft
Member
305 Points
67 Posts
Re: how to check Session null or not ?
Jun 27, 2008 03:02 PM|LINK
The best method is create a basePage like this:
public class PublicBasePage :System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["UserID"]==null)
{
Resoponse.Redirect("login.aspx");
}
}
}
devangopinat...
Member
28 Points
76 Posts
BasePage means MasterPage?
Jun 30, 2008 04:06 AM|LINK
hi
thanks for your help. Base page means Master page?
by
devan.g
SanjaySutar
Participant
1128 Points
692 Posts
Re: BasePage means MasterPage?
Jun 30, 2008 04:14 AM|LINK
No it means that u have created a new Class (let's Say New.cs )
and then u r supposed to inherit all your pages from this class(New.cs).
So New.cs can be called as Base class.
devangopinat...
Member
28 Points
76 Posts
how to construct a class
Jun 30, 2008 04:15 AM|LINK
hi
please guide me. how to construct a class and webpage as a parameter please guide me. or i need sample code for webusercontrol.
by
Devan.G
SanjaySutar
Participant
1128 Points
692 Posts
Re: how to check Session null or not ?
Jun 30, 2008 04:18 AM|LINK
Hi,
I totally agree with the suggestions given here.
but If u do not like deriving from a Base class created by u then
Create a class with static method (like Check() ) in it.
Then on page_load call this static method.