Preventing Postbacks

grandwazzoo

Newcomer
Joined
Aug 23, 2004
Messages
1
I have a page with an HTML button on it" "Back", which has an OnClick event that calls history.back();

This works just fine UNLESS I call up a modal / pop-up form, which I then close. This causes a postback of the initial form, adding it to the browser history so that I must now click the "Back" button twice to get back to where I want to go. This is annoying.

Ideas anyone?

Thank you
 
Can't you place an ASP Button and call the previous page using Server.Transfer or Response.Redirect from the server?
 
Well, if the popup window intentionally causes the main form to submit/refresh, then you could track a counter value in session or something, then use that with history.back(-X) to backup as many times as necessary.

The initial value for the backup counter would be -1. If the popup causes the main page to submit or refresh, then increment (decrement, actually) the counter to -2. When it comes time to fire your back button stick the backup counter value into your history.back(-x) output.

If your 'back' button does't do a postback to write out the 'history.back()' then you could have your page populate the onclick event for that button when the page renders (using the Attributes collection of the button). Just make the html button runat server, and in codebehind create the client-side click with something like (instead of putting the onclick in your aspx):

Code:
//  C#
//  In the appropriate location, the html button declaration:
System.Web.UI.HtmlControls.HtmlInputButton btnBack;

//  Add a client-side onclick to the html input of type button.
btnBack.Attributes.Add("onclick", "history.back(" + m_nBackCount.ToString() + ")");

I'm not that overly fond of this approach but one of my coworkers uses this technique a lot for 'back' buttons on pages that can submit to themselves several times before a user might want to back up. The reason I don't like (how he does) it is that he always does a postback to write out the history.back - and prefer to avoid doing postbacks that write out javascript to essentially redirect - and I'd rather not redirect instead - I'd rather avoid the postback all together if possible.

Paul
 
Back
Top