Go back to the previous page on click of custom Back Button - asp.net

I am using an image button and on click of it i want to go to visited page.
Now i am using - Response.Redirect(Request.UrlReferrer.ToString()),
It is going to previous page, but when i am in a page of some user details where the link is looks like - users.aspx?userid=25 and i visit some other page and click back(image button) i want to see the same userdetail page. How to track that. Please Guide.

Do you want your custom back button to always take the user to the same page as the browser's normal back button will? If so, you can use javascript to do that with something like
myBackButton.OnClientClick = "window.history.go(-1);return false;";

I would recommend using the Session object to store the url e.g.
Session("PreviousPage") = Request.Url.AbsolutePath // Or whatever....
You can then make use of the above in other pages to set the back button's url.
EDIT:
In response to your comment, are 100% sure you're saving the current url before moving to the next page?
Protected Sub btnNextPage()
Session("PreviousPage") = Request.Url.AbsolutePath // Or whatever....
Response.Redirect("nextpage.aspx")
End Sub

Related

Redactor - got to custom URL on submit

I have page with multiple forms, which are hidden based on what link is active. One of these forms is using redactor and I want to send the user back to this page with the redactor form open on submit. By default the page refreshes on submit and it shows the default form for that page. I couldn't find anything about this in the docs. If anyone knows how to accomplish this please let me know. Thanks
To solve this you need to use window.location.hash with this you can send a hash variable to the URL when you click on the link to get to your form.
The urlHash works as follows:
var UrlHashVal = window.location.hash.substr(1); //get the hash value and store as a var
$('form').hide(); //hide all forms by default
$('form#' + UrlHashVal).show(); //show the form whose id matches the hash value
What this does is enable you to send a link to someone, like http://ucanstayatthe.ym.ca#myForm and it will open that page with form#myForm on display.
Now all we have to do is enable this urlHash to work within the page as well. On the page we show/hide the forms based on links. All we need to do is write the formID to the URL from the link. To do this simply add the fromID to your href eg. "#myForm". Now when you click that link it will display #myform at the end of the windowURL.
This solves all of the problems because now when the page refreshes on the Redactor submit, it will reload the URL including the hashValue that you have written to it. SO instead of reloading http://ucanstayatthe.ym.ca it will reload http://ucanstayatthe.ym.ca#myForm which in turn will show the correct from.
Bingo

Using browser back button to navigate between the website pages

I have search this but didn't get useful information. In my website I have a order processing scenario. currently I am using a "back" button on page to go to the previous order page. I want to go back through the browser back button. I want the code which I added in page back button can run when I use the browser back button.
How I achieve this..?
try like this
$(document).ready(function(){
$('a.back').click(function(){
parent.history.back();
return false;
});
});
here the link is the one on which click you want to go back
or even better
have a look at this
History.go can do that:
parent.history.go(-1);

how to stop user to use back button

I have one button on page, page name abc.aspx . when user click on that button
it should redirect to finishwork.aspx page.
After finishwork.aspx page user must not go back to abc.aspx page. when user press back button in browser, he should be redirect to workallreadyfinish.aspx page
Disable caching on that pages and avoid caching the page.
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
location.replace() can be used to replace your page in the history.
There are many ways to accomplish this, but, as you are using ASP.NET and I'm going to assume, WebForms, why don't you make use of the <asp:Wizard> control?
You will be able to have a much detailed control over your steps and block the user to go back and all sort of nice things.
If you want to take the normal way, you can always warn the user that the page will no longer be valid using a javascript event.
You can also make use of hashing and submit the form by an ajax call instead of a normal POST
You can write a cookie once the form is submitted and the next time, show such warning, so even if the user goes back and press the Submit button again, you will not care. Remember to erase the Cookie on if (!Page.IsPostBack) { ... }
Having your user workflow around the browser back button is not such a good idea.
Browser back buttons are not entire in your control.
If you want to provide a logical back in your application, use a back button in your application.
If you only want that a user cannot go back to a page, you should tell the browser not to cache it set the page to expire.
You can use javascript function for this:-
function disableBackButton()
{
window.history.forward();
}
setTimeout("disableBackButton()", 0);

Validation using navigation

I have some pages in my website and a left menu control. Control helps to navigate from one page to another.
My query is -> While user try to navigate to another page, I want to impose some validation like in the current page if the form is not saved, user will be asked to save it by using a confirm messagebox and if user presses no button, then user will be allowed to navigate otherwise, system will first save the details and then navigate.
Edit - My page is a content page, I meant, this is using a master page.
Use the following steps
window.onbeforeunload = confirmExit;
and a function that stops/continue the page execution.
function confirmExit() {
var email= document.getElementById("email");
if (email.value != "")
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
The way I would do this is to have an onbeforeunload javascript event fire which gives the user the choice to save the form. I personally would also poll the form saving data back whist they are completing it. I think this is the method SO uses.
There is a pretty decent example over on Code Project that may help http://www.codeproject.com/KB/aspnet/AutoSaveFormData.aspx
EDIT:
If you only want to call the save method you can mark it with the [WebMethod] filter and call it using XmlHttpRequest or jQuery's $.post

Implementing Back button functionality in asp.net

i need to implement a back button for my asp.net website.I am able to use the javascript method to acheive my requirement.But using this method sometimes I need to click on the back button multiple number times to go back to the previous page.It may be because we are using jquery tabs in our website.To focus on a particular tab,other than the 1st tab on page load I am using Page.ClientScript.RegisterStartupScript(....).So I am unable to take the user back to the previous page with just one click.
I also tried with asp.net-C# methods mentioned in the following link.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=89
I am able to go back to the previous page, but its state is being lost.Could someone please help me in acheiveing my requirement?
Details:
I have page1.aspx,page2.aspx(which contains jquery tabs view/edit).
In the page1.aspx there are 2 buttons(View,Edit).If I click on view button it takes me to page2.aspx View tab(1st tab) and if I click on the edit button it has to take me to page2.aspx with Edit tab loaded.both View/Edit tabs contain back button.
Also from the View tab I can navigate to the Edit tab,by clicking on another Edit button present in it.
Thanks.
The methods you have covered in your question are essentially what is available to you.
You can either
1. Provide a link that uses javascript to make the client go back a page.
2. Provide a link that posts back to the server that redirects you back a page.
I am not sure why the jquery in your webform as described in your question is causing you to click more that once to go back. If you know that it will always take 2 clicks to go back you could try this method:
javascript: window.history.go(-2)
When you are using the postback/redirect method you will always be using a http GET method to retrieve the page you are returning too. If you want to maintain state you will have to do this manually i.e. save the values when leaving the page somewhere, like session or a temporary database, and when returning to the page, during the page load, check to see if the user has these values saved and pre-populate them.
I've done something similar (with automatic redirections though) and I had to keep track of the number of pages to go back in my ViewState (or Session if you're jumping from page to page):
code-behind
public void Page_Load()
{
Session["pagesToGoBack"] = ((int)Session["pagesToGoBack"])++;
}
mark-up:
<input type="button" value="Back" onclick='javascript:history.go(<%= Session["pagesToGoBack"] %>);' />
Be careful to reset the session variable when needed
Made me feel a bit dirty but it worked :)

Resources