ASP.net Redirect to the calling page - c#

I have a page that calls another page with some query string parameters. I want to return back to that page after clicking on a button.
I have to mention that I write that code in a user control and I don't know what page called that second page.
Is there something like Back button in browsers?

Simplest way use javascript on client side with
window.back();
For server side you need to save the url referer in page_load:
if(!Page.IsPostback)
{
ViewState["GoBackTo"] = Request.UrlReferrer;
}
and on a button click using Response.Redirect:
Response.Redirect( ViewState["GoBackTo"].ToString() );
edit: please note ppumkin's comment below!

You could look at Cross Page Posting.
Alternatively, if you are generating the link programatically you could include the returnUrl in the url e.g. http://localhost/secondpage.aspx?returnurl=firstpage.aspx
You can then read this querystring parameter in the secondpage and perform as redirect back once your work is done.

You can use the Request.UrlReferrer, but it is not necessarily sent from the client all the time:
Response.Redirect(Request.UrlReferrer.AbsoluteUri);

put this line of code on the page load event
Btn_Back.Attributes.Add("onClick", "javascript:history.back(); return false;");

Related

ASP.NET Page Cycle Confusion

If I have a button on my ASP.NET page which will take you to another page but that page will do something and then will send user back using
Uri uu= Request.UrlReferrer;
if (uu!= null)
Response.Redirect(uu.ToString());
Now Which page event can I use so that when other pages displays I can display a message box.
In short I am running my custom code in a "aspx" page where user is directed on button click, and then after custom code I am sending user back to old page, but it happens so quickly that user doesn't realize that he went on another page, now I want to display a message box after redirect on same page user started from, what to do :S !
More Information
EDIT
Sorry guys but I can't make changes to ASP page where button is at all
:(
I'm a bit unsure about how the Request.UrlReferrer gets set. I think it's a browser implementation detail. So I wouldn't trust on that.
I would go for something like
A.aspx -> Redirects to -> B.aspx
B.aspx -> Redirects to -> B.aspx?message=1
And check if message=1 is set.
But if you want to use the Request.UrlReferrer it should be accessible on Page_Load
If you use it this way, it'll never appear to the client.
Maybe try redirecting back using javascript with a delay so user can be informed
The best thing to do it's add a flag to the redirected page so you can show something special when the flag is turned on
Uri uu= Request.UrlReferrer;
if (uu!= null)
Response.Redirect(uu.ToString() + "?Message=DataHasChanged");
and then in the ASP page
<% if (Request.QueryString["Message"] == "DataHasChanged") { %>
<div class="alert">The data has changed. Please review it or whatever</div>
<% } %>

asp.net: how do i prevent users from posting the same data multiple times

i have a little asp.net web application.
it is a front end to a sql-server-2008 database.
after they fill out all the data, they will press submit and the data will be sent to the database.
after this point if the user refreshes the page, the data is posted again!!! how do i disable this from happening?
Send a Location header back to the user that redirects the browser to another page: refresh will then reload that other page rather than resubmit the old form.
This is caused by the last request being a POST request to the page, what do you need to do is a redirect so the last request becomes a GET.
After you have handled the post data you can just do a redirect to the same page with:
Response.Redirect("~/ThePage.aspx");
This will prevent you from presenting a message to the user straight from the code behind, if you want to present a success message using this method you will need to add a querystring or something similar:
Response.Redirect("~/ThePage.aspx?result=success");
And then check on the page bind if the querystring to present a success message is set, such a check could look something like this:
if (Request.QueryString["result"] != null && Request.QueryString["result"].ToString().ToLower() == "success")
{
//Show success message
}
Another solution which probably is superior but might require some more work is to wrap the form in a updatepanel, you can read more about it here: http://ajax.net-tutorials.com/controls/updatepanel-control/
An updatepanel will make the form submit with AJAX instead of full postback.
You need to follow the Post/Redirect/Get pattern which is explained on WikiPedia and alluded to by Femi. In your code after you've done your processing do a Response.Redirect to the desired page.
See this article about the PRG pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get
In short, after the user POSTs (submits) data to your server, you issue a Response.Redirect to have the users browser GET a page. This way, if the user presses the reload button, it is the GET request that is repeated.

Change url on server side

When user clicked on A tag ( <a href='showSomething.aspx?id=11&key=fixed'>Bring Something</a> ) showSomethingPage.aspx will be shown with URL parameters. But I want to change URL from showSomething.aspx?id=11&key=fixed to showSomething.aspx?key=fixed in despite of shown the thing with id 11.
Try saving the ID in the session state and then using Response.Redirect.
Use a redirect:
C# code (put this in Page_Load or something):
Response.Redirect("showSomething.aspx?&key=fixed");
If you just want to hide the URL from the status bar your link, use the onclick event to open the URL:
a href="javascript:void(0)" onclick="window.location='showSomething.aspx?id=11&key=fixed'"
Hope it helps you. ;)
You can save the id in the session and then redirect to showSomething.aspx?key=fixed. then showSomething.aspx?key=fixed will read the session and file the id=11 and show it.

Javascript alert() after Response.Redirect

I am calling this in my code behind:
(test.aspx)
Response.Redirect("~/Default.aspx");
I want to include a javascript alert after/before being redirected to Default.aspx, is it possible?
I'm doing this because I'm passing a value to another page (test.aspx) and that page checks the db, if reader HasRow(), then redirect to Default.aspx.
The way to do that is display the alert with javascript and then do the redirect with javascript as well:
ScriptManager.RegisterStartupScript(this,this.GetType(),"Redit","alert('asdf'); window.location='" + Request.ApplicationPath + "Default.aspx';",true);
Lets take a look at what happens when you call response.redirect()
Servers sends HTTP response 302 to browser including the url to navigate to
browser doesn't display content of response and instead gets the content from the url specified
if the request to the new url succeeds, then it is displayed.
Now looking at this, we can deduce that it is impossible to tell the browser to do a alert() from the page that issues the redirect because its content (if any) is discarded.
It is possible to accomplish what you want from the page that you are redirecting to. To do this, just check Request.UrlReferrer to check if you were redirected from the correct page, then display the alert when appropriate.
example:
Page1 redirects to page2
Page2 check if Request.UrlReferrer is equal to page1
If equal, display alert
If not, do nothing special
Another approach is do the alert first then do the redirect from javascript. window.location.href = newurl.
if you have display something message on Defaul.aspx page you must declare it. Because when you use redirect your page is rendering from top. You must set before redirect Sesion state on something flag and on Default.aspx page you must insert section who been added when this Sesion state been set.

ASP.NET Postback and URLs

I have a page that requires the user to go through several steps, however step is performed on the same ASPX page with different panels being displayed.
However this is a requirement that each step has a different URL, this could be a simple as a query string parameter, for example:
Step 1:
/member/signup.aspx?step=1
Step 2:
/member/signup.aspx?step=2
Step 3:
/member/signup.aspx?step=3
However I don't want to have to redirect the user to the new URL each time they continue to the next step, this would involve a lot of redirecting and also a switch statement on the page load to work out which step the user is on.
It would be better if I could alter the URL that is displayed to the user when the original request is sent back to the user, i.e. the user click "next" on step 1 the page then does some processing and then alters response so that the user then sees the step 2 URL but without any redirection.
Any ideas would be greatly appreciated.
Could you convert your Panels into steps in a Wizard control?
It would be a little more complicated than you probably want, but you could achieve this effect with the PostBackUrl property of the submitting button. I'm assuming each panel has its own "submit" button, and they could all use this property to "advance" the process. The drawback is that in order to get to submitted controls, you'd need to use the Page.PreviousPage property in order to access any controls and their values.
You could programmatically alter the PostbackUrl property of your 'Next' button on each Page_Load, based on the query string value. This is a bit strange though, as you wouldn't be able to use a standard event handler for the button click, and you'd have to use the PreviousPage property of the Page to get the data from the previous tab.
I'd say challenge this requirement. Why would anyone need to jump into the middle step? If it's a case of displaying the progress to the user, do this on the page, not in the URL.
You require that each step has different URL, than Response.Redirect is the only option. As you want to avoid the redirection, you can use IFrame but IFrame URL is not visible to user on his browser. I think redirect option is ugly(for both SERVER and CLIENT) as in this case, you first post on the page and than get that page. The best solution is POST BACK with some varible tracking step.
You could implement a form or url rewriting so that your urls end up being
/member/signup/step1/
/member/signup/step2/
/member/signup/step3/
To do this use the
HttpContext.RewritePath method which means you can rewrite /member/signup/step1/ to /member/signup.aspx?step=1 for example. See an example here.
I would also use the PRG (post request get) pattern so that each next link posts the form data of that step to the session then redirects the user top the correct next url. this will ensure that the user can navigate back and forth through the steps safely and also the url will remain intact in all your posts.
Check out server.transfer

Categories