Cross-page posting. Is it a good pratice to use PreviousPage in Asp.net?

The cross page posting is a helper to post some data to a different page and still have the asp.net code behind functionality.

Why is this exist ? because asp.net have a limitation of one and only form per page. But actually to an html page you can have many forms and many different post to different pages.

So to give a tool to that case, is let you set a second page to post the data, and you setup this on the Button (and not by placing second form), and from there is solve this issue, to post the data to a different page.

For example… with out asp.net and with simple html on a page you can do that.

<body>
<form method="post" action="samepage.html">
   Username: <input type="text" name="user" />
   <input type="submit" value="Submit" />
</form>

<form method="post" action="page_b.html">
   email for news letter: <input type="text" name="email" />
   <input type="submit" value="Submit" />
</form>
</body>

To solve a situation like this, and because asp.net not allow two forms at the same page, gives this option.

<body>
<form id="form1" runat="server">
Username: <asp:TextBox runat="server" ID="Name" />
<asp:Button runat="server"/>

email for news letter: <asp:TextBox runat="server" ID="email" />
<asp:Button runat="server" PostBackUrl="page_b.aspx" />

</form>
</body>

In the second case, you have one form, but you set the PostBackUrl to a different page, and from there asp.net still handle the data on code behind direct on a second page (with out redirect).

I hope this example gives you and an idea where to really use the previous page. Also what is more usually is the Redirect, how ever there are case that you need to have the result to a different page. So its per case if you use it or not.

Leave a Comment