I have two pages MainPage.aspx and ChildPage.aspx. From main page when i click a button i redirect to ChildPage.
If i give the address of ChildPage directly on browser, i do not want to load it directly instead i want to redirect to MainPage.
the ChildPage must be loaded only if it is loaded from the MainPage.
How do I find from where the ChildPage.aspx is loaded. how to find the parent page of it or from where it is loaded.
can we try something in the below code
if (!IsPostBack)
{
if (finding_source)
{
Response.Redirect("MainPage.aspx");
}
}
You can use Request.UrlReferrer.AbsolutePath to see the previous page.
if (!IsPostBack)
{
if (Request.UrlReferrer != null && Request.UrlReferrer.AbsolutePath == "/MainPage")
{
//do what you want
}else{
Response.Redirect("~/MainPage.aspx");
}
}
TIP But be careful with using it with postbacks since it will change the value of Request.UrlReferrer to the current page during a postback.
Even though your question is not clear , here i am trying to give my solution.
MAINPAGE.ASPX BUtton Click
protected void lnkRegister_Click(object sender, EventArgs e)
{
Session["MainPage"] = "true";//Encrypt it if u wish
Response.Redirect("childpage.aspx");
}
CHILDPAGE.ASPX PAGE LOAD
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Session["MainPage"] as string) && Session["MainPage"].Tostring()=="true")
{
//proceed
}
else
{
Response.Redirect("mainpage.aspx");
}
}
GLOBAL.ASAX
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
Session.RemoveAll();
Session.Clear();
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}
it is little difficult to control browser shutdown or close...but you can workaround global asax file when your application shutsdown.
Related
Clarification: I am chilean, so my english is not perfect, sorry for the misspellings.
Hi, I am working with an image in c#.
I try to put an example image when the page open the first time, I used the post back for this, but when I press a button, execute the code in the post back section (which is right), after that it execute the Button code, but then, it pass again for the Page_Load method, and execute the "not post back" section, and i dont know why.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//Is post back
}
else // Is not post back
{
//Make things only when the page is open for the first time
}
}
I usually only use (!IsPostBack) on PageLoad for doing initial data loads or validations(like settings for users).
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (userIsAdmin)
{
button1.Enabled = true;
}
}
}
You could refer to the link for the explanation for PostBack https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8
I have a property as shown below
private int Step
{
get { return (int)Session["step"]; }
set { Session["step"] = value; }
}
In the Page_Init method, I am initializing it as below
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
Step = 0;
}
}
In the click event of my save button, I am trying to reload the page as if it was the first time the save button was clicked. If it was the second time the save button was clicked, then redirect to another page.
protected override void cmdNext_Click(object sender, EventArgs args)
{
this.SaveViewModel();
Step++;
if (Step > 1)
{
base.cmdNext_Click(sender, args);
}
else
{
Response.Redirect(Request.RawUrl); //reloading the page again
}
}
The issue is that on the first time the page is reloaded and the Page_Init method sets the variable to 0 again so it never goes past through 1. Can someone please tell me how I can load the same page on the first button click and move on to some other page after the second button click?
Thanks
You can simply check if the Session is null before initializing
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack && Session["step"] == null)
{ Step = 0; }
}
I have 2 pages home.aspx and admin.aspx
After successfully logging into admin.aspx when i click back button of browser, it does redirect to home.aspx but that i don't want.
I am checking session variable persistence on home.aspx but for some reason its not working!!
Here's the code
home.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["aname"] != null)//should work as session will not be null!
{
Response.Redirect("admin.aspx");
}
} //.....some code..after this
if (dt.Rows.Count != 0)
{
Session["aname"] = TextBox11.Text;
Response.Redirect("admin.aspx");
}
admin.aspx.cs code
protected void Page_Load(object sender, EventArgs e)
{
if (Session["aname"] == null)
{
Response.Redirect("home.aspx");
}
} //some code after this..
protected void logoutbutton_Click(object sender, EventArgs e)
{
Session["aname"] = null;
Session.Abandon();
Session.Clear();
Response.Redirect("home.aspx");
}
NOTE:(things working fine)
1.login working sucessfully
2.logout working sucessfully
3.back button is disabled once loggedout(not going on admin.aspx)
Issue:
When logged in i.e. on admin.aspx ,on clicking back button it redirects to home.aspx which i don't want. i expect it to remain on same admin.aspx
ok.. finally trying all your solutions..this code worked on adding in my masterpage (in head tags)
<script type = "text/javascript" >
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
</script>
full details on this page
You can push the Window History forward to prevent the back button. This has work for me in most cases. Include this JavaScript on your Admin.aspx page.
$(function() {
window.history.forward();
});
I am having 2 pages name as :
Abc.aspx
pqr.aspx
Now on page load of Abc.aspx i am doing this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["Alert"]))
{
if (Request.QueryString["Alert"] == "y")
{
//Here on redirection from Pqr.aspx i will display Javascript alert that your "Your data save"
}
}
else
{
//Dont do anything
}
}
}
Now from pqr.aspx page i am redirecting to Abc.aspx and passing query string on button click:
protected void Save_Click(object sender, EventArgs e)
{
//saving my data to database.
Response.Redirect("~/Abc.aspx?Alert=yes");
}
But what is happening is if anybody enters url like this in browser then still this alert is coming:
http://localhost:19078/Abc.aspx?Alert=yes
Then still this javascript alert box comes.
What i want is after redirecting from my Pqr.aspx page only this alert should come.
How to do this??
In Asp.net there is an object named Request.UrlReferrer.With this property you can get the previous page from which you come to the current page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["Alert"]))
{
if (Request.QueryString["Alert"] == "y" && Request.UrlReferrer != null && Request.UrlReferrer.LocalPath == "/pqr.aspx") // if the root is same
{
//Here on redirection from Pqr.aspx i will display Javascript alert that your "Your data save"
}
else
{
//Dont do anything
}
}
}
}
I have an .aspx page that has a page_load as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.Name != "")
{
....
}
else
{
FormsAuthentication.RedirectToLoginPage();
return;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
....
}
The issue I am noticing is that when the user clicks the submit button it goes though the Page_Load (goes though the else) and then tries to run though the protected void btnSubmit_Click(object sender, EventArgs e).
Is there a way to make it redirect to the login page and not continue to the next void?
This is NOT an issue. This is the normal ASP.NET Page Life Cycle.
Controls events fire right after the Page_Load event.
Note that the FormsAuthentication.RedirectToLoginPage method does not end the request by calling HttpResponse.End. This means that code that follows the RedirectToLoginPage method call will run.
Assuming you want to stop further processing from within the Page_Load (which I believe is your intention with return):
protected void Page_Load(Object sender, EventArgs e)
{
if (/*I want to kill processing*/)
{
// Method one:
Response.End(); // Though admittedly ugly
// Method two:
this.Context.ApplicationInstance.CompleteRequest();
return; // return as normal (short-circuit)
}
}