views:

1101

answers:

3

i want a handler to redirect to a web-forms page, pre-filling in the values of some controls on the form.

i tried setting my current Request.Form data:

if (theyWantToDoSomething)
{
   //pre-fill form values
   context.Request.Form["TextBox1"] = "test";
   context.Request.Form["ComboBox1"] = "test 2";
   context.Request.Form["TextBox2"] = GetTheTextForTheThing();

   //tell the client to go there
   context.Response.Redirect("~/SomeWebForm.aspx");
   return;        
}

But i get an exception that Form values are read only.

What would be a way to send the client to another page, pre-filling form data?


Answer

i used the Session state to store values. It's important to note that by default a Handler doesn't have access to Session (the Session object will be null). You have to tell IIS to give you the Session object by adding the IRequiresSessionState marker interface to your handler class:

public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
   public void ProcessRequest(HttpContext context)
   {
      ...
      if (theyWantToDoSomething)
      {
         //pre-fill form values
         context.Session["thing1"] = "test";
         context.Session["thing2"] = "test 2";
         context.Session["thing3"] = GetTheTextForTheThing();

         //tell the client to go there
         context.Response.Redirect("~/SomeWebForm.aspx");
         return; //not strictly needed, since Redirect ends processing
      }
      ...
   }
}
+3  A: 

A few ideas that might get you started:

  1. Pass the values in the query string
  2. Store the values in the session state or in a seperate cookie
  3. Store the values in HttpContext.Items and use Server.Transfer instead of Response.Redirect
Jørn Schou-Rode
+4  A: 

You can only populate your Response, the Request is input data and is indeed read-only.

If you are using ASP.NET, there are a variety of ways you could accomplish what you need:

  • The best way would probably be to pass the data you need to be pre-populated to SomeWebForm.aspx via the Session object, and on that pages Load method, populate your form. Keep in mind that when you do Response.Redirect, a 302 response is sent to the client with the URL the client should redirect to. The process is transparent to the user...but there is a full round trip involved.

  • Another alternative to populating the users Session would be to add GET parameters via a query string to the redirect to SomeWebForm.aspx.

  • If you need to transfer processing to the SomeWebForm.aspx page without round tripping, you could use Server.Transfer. This will transfer execution from the current page to the page you choose...however, this can cause some odd behavior on the client end because the URL does not update. As far as the user is concerned, it will still appear as though they are on the same page they started on.

jrista
To add to your statement about the redirect - the 302 sent to the browser initiates a GET request on the new URL, not a POST, so your options are to either store data in a query string or in the session. You can't do a form style POST with a redirect. You implied this in your post, but wanted it to be clear to Ian Boyd.
Matt
There's a trick you can play. You can redirect to a page which has JavaScript on it that will do a POST to the page you really want. This is how SAML SSO works: redirect, passing data in a hidden field; the JavaScript then turns around and POSTs this data to the desired page.
John Saunders
Great additions from Matt and John. The JavaScript trick is a pretty good and common one when you really need to do a POST, rather than a GET.
jrista
i used sessions, works well enough.
Ian Boyd
+1  A: 

Another approach that hasn't been mentioned yet is using Server.Transfer makes it possible to use the Page.PreviousPage property, allowing access to the controls on the page that transferred control.

As jrista mentioned though, using Transfer doesn't update the URL shown to the user, which may or may not be an issue. For example, a user can't precisely bookmark a page they got transferred to since the URL will be that of the original transferring page.

Ahmad Mageed