0

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:

  1. I gather there isn't a PostUrl form property, or is there?

  2. 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's PostBackUrl as well) work?

  3. The data must be POSTed (i.e. not encoded in the URI string as in a GET request).

  4. 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?

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
  • Did you look at/try any of the other answers on the various questions you've looked at? For instance [this answer](https://stackoverflow.com/a/2802848/215552) on [Response.Redirect with POST instead of Get?](https://stackoverflow.com/q/46582/215552) shows simply constructing a normal HTML `form` with an appropriate `action` attribute... – Heretic Monkey Apr 01 '19 at 19:53
  • @HereticMonkey yes, I seen *all* of them and tried some - not that one in particular, thought, because it's not straightforward enough (mostly, the same reason we're not at ease using `session`). Also, that's not strictly codebehind - although at this point I suspect this **can't** be done in pure codebehind. And if that's the case, I would prefer changing the original form's `action` (via javascript) and forcing a new post instead of marshalling every single form element into a new `form` individually. – Marc.2377 Apr 01 '19 at 20:03

0 Answers0