A colleague has been developing an ASP.NET Web Forms application and he wants to submit information entered in a form from one page to another.
He asked me how it should be done, and despite not having a whole lot of experience with ASP.NET webforms, I answered it could be done like a simple HTML form post. However we are having a hard time implementing this in practice.
Both pages use the same Master Page, and the masterpage defines one <form> element. In the first page (or should I say, webform), there are <asp:TextBox runat="server" ...> elements and one <asp:LinkButton runat="server" ...>. The button has OnClick="lkbSend_Click".
In the codebehind (at the lkbSend_Click method), my colleague has to perform some pre-validation before submitting the information to the second page, hence using the PostBackUrl attribute for the LinkButton (first suggestion I found[1], [2]) is not allowed, at least not from the aspx markup.
After more research we found suggestions[3] of doing a POST request followed by a redirect from the codebehind, but this doesn't work as we'd expect because these are two separate operations - the POST works alright, but when the user is redirected we no longer have Request.Form["..."].
A third idea I came across[4] was adding the information to Context.Items and then using Server.Transfer; however we can't do that because the URL must change between pages.
For the time being my colleague is storing the form contents in Context.Session, but neither of us are happy with that approach as we find it doesn't scale so well.
I thought about changing the linkbutton's PostBackUrl property from the codebehind (at the end of the lkbSend_Click method) and then invoking a new form POST. Would that be possible, and if so, how?
I tried:
btnSendEmail.PostBackUrl = "<another page's URL>";
btnSendEmail_OnClick(sender, e);
But, not with much surprise, this generates a StackOverflowException.
In case that wouldn't be possible, is there another way to go?
Additional notes:
I gather there isn't a
PostUrlform property, or is there?I've just read[5] that "You definitely cannot POST and redirect in the same time". Would that be correct? If so, how does HTML's
<form action="...">(and presumably, ASP'sPostBackUrlas well) work?The data must be POSTed (i.e. not encoded in the URI string as in a GET request).
I've seen a suggestion that a HTTP 307 redirect would do the trick[6], but it is not reliable.
Thanks,
[1] How to pass information between web forms in asp.net
[2] asp.net 4.5 PreviousPage is null with cross page posting using a master page
[3] Redirect to another page using Post method from Code behind
[4] Action PostBackUrl Programmatically using C#
[5] Can I set a POST variable before redirecting to a new page?
[6] A good way to redirect with a POST request?