Using browser back button to navigate between the website pages - asp.net

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);

Related

is it possible to not reload user control when redirect to a page?

I have created a user control for side menu navigation and when user clicks on one of the item on a menu, it displays details of the company and information. Now it is redirecting to different page for each company using passing parameter (ex. company.aspx?ID=10) and everytime when I clicked, it refreshes whole page. I would like to keep side menu user control without refresh as there is a live search option.
Is this possible? or should I change opposite way to do it? should be the side menu in a page and company info in a user control??
If you want to do a proper way using Ajax, you'll need to change the entire site structure. Here is a quick and dirty way of doing it using jQuery.
<script>
$(document).ready(function () {
$("#left-pane a").click(function (e) {
e.preventDefault();
loadUrl($(this).attr("href"));
});
});
function loadUrl(url) {
$("#right-pane").load(url);
}
</script>
I think what you're talking about is partial page update, where, rather than posting and reloading the entire page, you only reload part of it.
Here's a good article: Understanding Partial Page Updates with ASP.NET AJAX

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);

Go back to the previous page on click of custom Back Button

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

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 :)

Close Browser Window after Form Post

Here's my situation.
I have a button on my ASP.NET webform. This button creates a new browser window pointing to a page which has a lot of hidden fields (which are dynamically generated). This form submits itself to SQL Reporting Services on the bodies onload event. This works fine and the report is displayed in this new window.
However, now I want to still POST a form to SQL Reporting services but I want to get back an excel spreadsheet. So I add another hidden input with a name of rs:Format and value of Excel. This works and the user gets the option to download the excel file.
However they are now stuck with the extra window that was created. How do I get around this? I've tried creating the dynamic form and POST in the same window, but then they see the (empty) page with the form, and not the page they generated the report from. I've tried closing the window that I've created but I don't know where to put the javascript to do this. If I put it on the onload, then the window closes without the form being submitted.
Any ideas for what to do here?
Edit: What I was doing here wasn't the best way of getting the result I needed. I ended up using a WebRequest to get the excel report from Reporting Services instead posting a form, therefore I didn't need the second window afterall.
Don't close the browser. It belongs to the user, even if you opened it. Closing it can make them mad.
Do redirect to a page the communicates to the user that you're done with the window. There you can provide a (javascript-based) link that make closing the browser a little easier if you want, though closing a browser window is generally pretty easy.
By the way, if the popup doesn't contain any useful output, what you may want to do is submit your form into a small Iframe within the page. This way there's no need to close a window, as the frame can be made invisible.
When user wants an Excel file, there's no need to pop up another window. I assume selection of Excel file or HTML report is done in some HTML control like a radio button or a checkbox. So, before doing anything, check the value of that radiobutton/checkbox with javascript and do the appropriate action. Something like:
function getReport(excelFormat)
{
if (excelFormat)
document.form1.target = '_blank';
else
document.form1.target = '_self';
document.form1.submit();
}
What if the button did an Ajax request back to the original page and got the hidden field values. You could then construct another form on the page with the hidden fields using javascript and submit it -- with the download option. Since the request will return an application/ms-excel file, it shouldn't refresh the current page but the download should still occur. You'd need to make sure that the button click didn't cause a postback by returning false from the client-side function. Note that this only works if the post of the generated form results in a download, not a new html page.
<script type="text/javascript">
function submitReport( button ) {
PageMethod.SubmitReport(onSuccess,onFailure,{ control: button });
}
function onSuccess(values,ctx) {
var form = document.createElement('form');
form.action = reporting-services.url;
form.method = 'post';
document.body.appendChild(form);
.... add hidden fields to form from returned values
form.submit();
document.body.removeChild(form);
}
function onFailure(error,ctx) {
... pop up some error message....
}
</script>
...
<asp:Button runat="server" id="reportButton" ClientClick="submitReport(this);return false;" Text="Report" />
Generally it's ok to close any popup window that your app has created.
This can be done with window.close() (which will pop up a confirmation if the window was not created by script).
If you want to be sure that the download is successful before closing the window, you will need to perform some server-side magic - have your server keep track of the download in progress, and poll it via AJAX from the popup window until the download completes.
Once the server tells you it's done, the window can be closed.

Resources