I want to log a users session on my site. I was hoping to just set a session variable. But the initialisation of it is giving me problems.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["SESSION_GUID"] == null)
{
Session["SESSION_GUID"] = Guid.NewGuid().ToString();
Response.Redirect(Request.Url.ToString(), true);
return;
}
lnkUpload.Visible = (Session["LOGGED_IN"] != null);
btnLogout.Visible = (lnkUpload.Visible);
}
It seems that the masterpage is creating the variable OK, but the rest of the pages load (ones using that master page). So in my log, I am seeing TWO hits. One with no session ID set, and the second hit has a session id.
Any idea why the child page of this masterpage is loading twice? This only happens on the first request to the site. After that, the session is working fine and we only get one hit.
The Page_Load event of the content page will be called before the Page_Load of the master page as seen in steps 6-7 in the ASP.NET Page Lifecycle
The master page Page_Init event is called before the content page Page_Init event though, so if all you're doing is checking for Session, probably best to handle it in the Page_Init of the master page and redirect there. This could cause problems based on how other code in your project looks but conceptually, one of the first things you are wanting to do is check Session and handle properly so, you don't need page/controls to load before handling the redirect.
Try doing a Response.End() in the master page. This will halt the remaining markup from being rendered.
Related
I have a page that has...
Page_Load(){
if (Session["UserName"] != null){
//code for security questions.. i have to go into this logic on every postback to check the security answer.
}
else{
//sends to a page i don't want it to go to
}
}
method(){
...
Session["Username"] = null; //if it reloads the page_load from this page, it sends it ot a page i don't want it to go to.
Response.Redirect("nameofpage.aspx");
}
The problem is that it does not go straight to the new page. It is going through the Page_Load of this page again, and I need it to go to "nameofpage.aspx". Is there a way to send it to the next page without using Server.Transfer?
Give a look at Page.IsPostBack property to check if a the page is being rendered for the first time or is being loaded in response to a postback.
The problem you're having is the redirect does a post back. You can use the function IsPostBack to check to see if it's the first time a page is loaded or if its being run on postback.
if(!Page.IsPostBack){
// Do stuff
}
Your problem is that the page is doing a postback. That is why your page is firing twice I believe. In your code you should do:
if(!this.IsPostBack)
//continue with code
else
//something else
Also do a check on your #page directive and be sure that AutoEventWireup="true" isn't there. This typically is a root cause of the postback issue or why page_load would be called twice.
Sorry guys, it was a lousy javascript _dopostback on the front side causing multiple postbacks. :(
*bangs head on desk and mutters... other ppls code
I have a form inside a Usercontrol, & calling this from a page say index.aspx.
After submitting the form and clicking refresh, it again causes postback.
So, I tried to maintain a flag in a viewstate as '1' after data saved and '0' when it is initial page load.
But why does the viewstate change its value to 0 with refresh.
How to resolve this.
MyForm.ascx.cs
protected void Page_Load(object sender, EventArgs e){
if(!IsPostBack){
ViewState["DataSaved"] = 0;
}
}
protected void btnSubmit_Click(object sender, EventArgs e){
if (Page.IsValid && Convert.ToInt32(ViewState["DataSaved"]) == 0){
//save data;
ViewState["DataSaved"] = 1;
} else{
ViewState["DataSaved"] = 0;
}
}
On refresh the page is reverted as it was initially (you came to this page via GET request).
So Viewstate is returned to its initial state and all changes made to it are lost. This is normal behavior. The behavior you expect is more like a session, so you might try using a session - it will maintain its state between requests.
Look at this for general understanding of page life cycle in .net:
http://msdn.microsoft.com/en-us/library/vstudio/ms178472%28v=vs.100%29.aspx
And this for explanation of Viewstate:
http://msdn.microsoft.com/en-us/library/ms972976.aspx
Both will be very helpful for you!
Good luck.
To make it simple, ViewState is saving inside user machine, and Session is saving inside server machine (access by cookies).
ViewState will be refresh whenever page IsPostBack or !IsPostBack
Session, however, will not.
In your case, i think you should use Session, instead of ViewState.
To understand more, you can go through ASP.NET Life Cycle. It's always good to learn from basic
Use a Session instead of the ViewState.
I have the following method that is called when reloading certain pages in my app. The point being to interrupt the current transaction, ask for authentication and call a specified function when coming back to the page to complete the transaction. State, and the method name are saved in Session in the interim.
After redirecting back to the page, the method executes and everything looks fine, but when clicking on a link to the same page it now just displays the same page, not a new page with empty fields etc. and, no page events fire. Eventually if I leave the page for long enough, maybe 5 minutes, it will refresh the page or clicking a button with a Response.Redirect call will do it.
Call the same page method normally w/o reflection and it looks to be fine. It seems as if using reflection may be keeping the page object in memory and not allowing it to be GCed. What is going on here? There are plenty of workarounds, but I'd still like to understand it for the future.
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
if (IsRestoredPageState && priorPageState.CallingFunction != null)
{
var completeAction = this.GetType().GetMethod(priorPageState.CallingFunction);
completeAction.Invoke(this, null);
completeAction = null;
}
}
when trying to access a search function button in my site master page while in the login page, the page just gets refreshed, using Server.Transfer instead throws the "Error executing child request for " error, accessing from everywhere else is okay, could this be I can't get out of the 'secure zone' while in the login page? Does this has anything to do with the web.config, I am not sure which portion of code should I look into either.
Please kindly advice. Thanks.
This is click event:
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
Session["txtValue"] = TextBox1.Text;
Response.Redirect("ProductSearch.aspx");
}
}
Your login page should not use the site master page. (Nor should any error pages). In that way, the various code you run in the master page will not need to run in the unauthorized context.
The web.config will always send the requests back to the login page if the user is unauthorized. If your code does run, you can find loops leading to stack overflows.
Is the page you're trying to navigate to (ProductSearch.aspx) protected (i.e. do you need to first login to access it)? If so, and if you are not yet logged in then any attempt to navigate to ProductSearch.aspx would automatically redirect you back to the login page (as you describe).
Cheers, Wayne.
First, I would make sure there is nothing in your ProductSearch page that would redirect you, maybe based on your session state or something. Also check the master page to make sure its not that redirecting you. Finally, if its still not working, I'd use the debugger and just step through your code.
I googled this about 1/2 a hour no hit's. Scenario is that, dynamic scripts are saved in string builder whose "string" representation is stored in session. It just happens that when user navigates away from one page to another the script[from session] gets registered using "RegisterStartupScript". The script is registered in PreRender event of the Page. So i would like to clear this script in session while the page navigates away btw rule out a option to create another session variable and clear previous one. It's a overhead :(
Why are you storing this in Session, do you need to maintain this script in between GET requests?
If only postbacks are relevant you could store it in viewstate as this is maintained only when doing a postback.
If you want this string to be available on GET requests too you might want to introduce a different variable which has an identifier identifying the page for which the script is generated. If the requested page doesn't match the control variable you will have to generate a new script.
How is the user navigating away from the page? Can't you use an ASP.NET button instead of a hyperlink, and then do a Redirect in code once you have cleared your session variable?
protected void btnDoSomething_Click(object sender, EventArgs e)
{
Session["Value"] = String.Empty;
Response.Redirect(strURL, false);
}
OR You could add a variable in the query string and check it in the Page_Load event of the target page:
Webform1.aspx?reset=true
Since I cant comment yet, use onUnload().
It fires on full postbacks too. Ajax postbacks dont fire!
What you need to do, is guaranty inside the onUload function that you only clear the session when you want. Like setting a variable isPostBack to true before the postbacks so onUnload sees the variable and doenst send a request to clear the session.
You may use the JavaScript onUnload() and call an AJAX service, that will clear the server side session.