Move Back command from server

M

moondaddy

Is there a way to make the browser move back to the previous page via code
behind on the server? The effect would be the same as the user clicking on
the browser's back button.

Thanks.
 
C

Chad A. Beckner

Can you just use a javascript script?

<script language="javascript">
history.go(-1)
</script>

Chad
 
S

S. Justin Gengo

No, because the server and client operate in a disconnected state that would
have to be done client side.

You could use a javascript sent to the client so that when it loads the page
sent from the server a back command would be fired.

The javascript command for that is: history.back()

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

Steven Cheng[MSFT]

Hi Moondaddy,

The "Back" button function is belong to the browser, generally we can only
use clientside script to directly call such function rather than via
serverside code. However, if we do have to do this by serverside codebehind
code of an ASP.NET web page, we can use the "Page.RegisterStartupScript()"
function to register a certain clientside script as
Chad has mentioned to workaround this. How do you think ?

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
M

moondaddy

Thanks to everyone who posted above. The initial logic for this must start
on the server because when certain conditions are met on the server, then
the page must go back to the previous page. I could pass a parameter into
the current page telling me what the last page was (the page that called the
current page) and then use a redirect back to that page, but that would
force the page to be reloaded. But if the client browser used the back
method then it could simpley pull the last page and its state from the
client cache with is ideal for this situation. So based on the comments in
the postings above I think a good solution will be to use the
"Page.RegisterStartupScript()" function to register a certain clientside
script as Chad and Steven Cheng mentioned, and then use then JavaScript
command for that is: history.back() as S. Justin mentioned. I haven't tried
it yet, but this sounds like what I need.

Thanks y'all!

--
(e-mail address removed)
S. Justin Gengo said:
No, because the server and client operate in a disconnected state that would
have to be done client side.

You could use a javascript sent to the client so that when it loads the page
sent from the server a back command would be fired.

The javascript command for that is: history.back()

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

moondaddy

OK so I register this javascript on the client to move back to the previous
page. how do I get it to execute on the postback?

--
(e-mail address removed)
moondaddy said:
Thanks to everyone who posted above. The initial logic for this must start
on the server because when certain conditions are met on the server, then
the page must go back to the previous page. I could pass a parameter into
the current page telling me what the last page was (the page that called the
current page) and then use a redirect back to that page, but that would
force the page to be reloaded. But if the client browser used the back
method then it could simpley pull the last page and its state from the
client cache with is ideal for this situation. So based on the comments in
the postings above I think a good solution will be to use the
"Page.RegisterStartupScript()" function to register a certain clientside
script as Chad and Steven Cheng mentioned, and then use then JavaScript
command for that is: history.back() as S. Justin mentioned. I haven't tried
it yet, but this sounds like what I need.

Thanks y'all!
 
S

Steven Cheng[MSFT]

Hi Moondady,

For example, if we have put the following script function in page template:
<script language="javascript">
function goBack()
{
window.history.go(-2);
}
</script>

To make it execute in the codebehind , just code as below in the post back
event:
private void btnBack_Click(object sender, System.EventArgs e)
{
string script = "<script language='javascript'>goBack();</script>";
Page.RegisterStartupScript("goback",script);
}

The reason why I used "2" in the window.history.go(-2); is because the when
the asp.net page is posted back, the post back state will also be added
into the browser's history list so we need to go back twice to navigate to
the previous page.\

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
M

moondaddy

Thanks Steven,
This worked great on a basic aspx page. However, I'm executing the code:
private void btnBack_Click(object sender, System.EventArgs e)
{
string script = "<script language='javascript'>goBack();</script>";
Page.RegisterStartupScript("goback",script);
}
from a user control on a aspx page that inherits a base page. Do you know
of any reason when I wouldn't run from a user control or on a page that
inherits from a base page? I looked at the page source and everything
looked OK meaning that the dynamic JavaScript was written to the page
correctly.
 
S

Steven Cheng[MSFT]

Hi Moondaddy,

Thanks for your followup. From your further description, it seems that you
implement the code mentioned in my last reply in a UserControl on a page
derived from a base page rather than directdly in a normal page's template,
and found it not work? Then, does it work if we use it in a UserControl on
a normal page(not derived from the base page). This can help confirm
whehter the problem is due to the page inheritance or the UserControl.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top