how do I return to the same exact page after posting a form?
-
20-09-2019 - |
Question
I'm posting data to a page called process.aspx that handles some business logic with the following code:
<%@ Page Language="C#" %>
<%
MyData.process(Request);
Response.Redirect("")
%>
this page I will be calling from a variety of pages. Is there some way of knowing from which page my form was submitted? I was thinking something along the lines of writing:
<form id="frmSystem" method="post" action="process.aspx?page=<%= %>">
However I don't know what to write in between the <%= %>
to get the current page name. Can anyone help please?
Solution
You can capture the calling page URL and hold it in Session
or ViewState
for later use.
For example, in Page_Load
,
Session["PreviousPage"] = Request.Url.ToString();
And then in your final event (perhaps Savebutton_Click or CloseButton_Click), you can do a redirect in either of these ways:
Server.Transfer(Session["PreviousPage"].ToString(), false);
or
Response.Redirect(Session["PreviousPage"].ToString(), false);
You can also get the URL of the calling page this way:
Request.ServerVariables("HTTP_REFERER")
OTHER TIPS
You could pass in it in via a property like the ReturnUrl
similar how a sign in page works. This is kind of how you are doing it up there.
You could also try to use the HttpContext.Current.Request.UrlReferrer
to see who referred you.
You can use Request.UrlReferrer.OriginalString
to get the URI of the referring page.