Search code examples
c#asp.netresponse.redirectform-post

how do I return to the same exact page after posting a form?


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