views:

587

answers:

2

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.

A: 

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 removed the opening bracket as the above code was not visible when I posted first time.)

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=

24x7Programmer
A: 

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.

Andrew Corkery
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.
Ken Browning
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.
Andrew Corkery