How do I implement the Post/Redirect/Get pattern in asp.net WebForms?
Asked Answered
T

2

4

So I understand the basics of the prg pattern. But I haven't come across a technique for making arbitrary data available to the "get" instance of a page.

For example, I might want to display different feedback messages to the user depending on their action which initiated the PostBack.

What I've been doing is sending an identifier as a query string parameter. This works fine but it introduces bookmarking issues and doesn't seem like it would scale very well. What if I needed to send all of the ViewState?

Unfortunately, I'm tied to WebForms at the moment and haven't been able to convince my organization to migrate to mvc.

Taco answered 28/5, 2009 at 6:51 Comment(0)
B
3

If the fields POSTed are being persisted before the Redirect and you need to access that data after the Redirect, I would append the identifier for the data record(s) on the query string as you mention. You could also specify a status for the request (for displaying messages etc). Then on the GET page you can read out the data and do whatever with it.

I don't see any other way to get around this as each page obtained by GET will not have access to the previous page's ViewState etc.

Using Server.Transfer will have the same effect as handling the POST on the original page.

You could use Session variables to store the POST data, but that stinks.

Buckthorn answered 28/5, 2009 at 13:16 Comment(2)
Yeah, I realize the information will need to be persisted. I'm looking for an optimal way of implementing that. As I understand it, Server.Transfer bypasses the "Redirect" part of prg.Taco
The only options that occur to me for implementing persistance in this scenario would be to store/retrieve the data in a database or file, or store the data in the Session object. You could store an object to represent the data in the POST, though storing primitive types in Session is probably more efficient.Buckthorn
R
0

I think you can still use the get method in the form element. In this case you won't be able to use the ID of your control in normal way. But you can use the Request.Params collection to get the viewstate.

Update: Sorry, I just tried again. and found that you can access your server control by its ID in codebehind. like:

Response.Write(text1.Text)

see the example:

the aspx page(only the form element):

< form id="form1" runat="server" method="get">
< div>
< asp:TextBox ID="text1" runat="server" />
< asp:Button ID="button1" runat="server" OnClick="buttonClick" />
< div>
< form>

NOTE: I used a space before each" <" otherwise the code above will not be visible.

the code behind page (only button click event):

protected void buttonClick(object sender, EventArgs e)
    {
        string text = Request.Params["text1"];

        Response.Write(text);
    }

when you click the button the url will look like this:

http://localhost:1157/WebSite1/Default.aspx?__VIEWSTATE=%2FwEPDwUKMTkwNjc4NTIwMWRkoJEtEHZ8lHQ53QllSkz8ncpEw80%3D&__EVENTVALIDATION=%2FwEWAwKf%2BPPUCwKTjKGwCgKs34rGBvus9oxN%2FQkHlkzpKUEwrbxHLM6u&text1=ashish&button1=

Rigi answered 28/5, 2009 at 7:41 Comment(1)
There's limits on the size of the querystring, circa 2kb. The viewstate will nearly always be way bigger than this.Execration

© 2022 - 2024 — McMap. All rights reserved.