Post to a Different Form

How do you use Web forms to post to another form?

asp.netNOW Q&A




Post to a Different Form

How do you use Web forms to post to another form?


By Josef Finsel


Q: I want to use a form, do validation on it, then submit the entire form to another page for it to do some work, display results, and so forth. How do you get all the form fields to pass from one page to another when you're using a server form?

- RS, Brighton, Mich.


A: This is a good question and, depending on your situation, the answer is either easy or difficult.


Before .NET, you simply would use the Method property of the FORM tag to post to the new page. You still can do that as long as your form doesn't run on the server. You can do this as long as you don't use any ASP controls in your form because they must be contained in a form that runs at the server. This means that, as long as you don't want to take advantage of the Web form-specific controls, there's no problem at all.


Any Web form controls that in your page require the FORM tag hosting them run at the server. And when a form is run at the server, it posts back to itself automatically and ignores the Method property if you've assigned it. But don't despair - it can be done, just not easily. The basic idea is to post the form to itself, save the values to session variables, and redirect to the new form, taking differing actions depending on whether the new form is being posted back or posted from somewhere else.


Start with a simple form to demonstrate this. You can download the code for this article, where you'll find two Web forms, PostSelfForm and PostOtherForm. Both forms have two input boxes, a couple of labels, and a submit button. Take a look at the button-click code on PostOtherForm:


Session.Add("FirstName", txtFirstName.Text);

Session.Add("LastName", txtLastName.Text);

Response.Redirect("PostSelfForm.aspx", true);


The first thing you need to do is take the data in the textboxes and add it to session variables. Then, redirect the browser to the form you're posting to. One thing the Web form controls do is keep the values entered. To mimic this, you need to load the session variables into the appropriate textboxes. But you only want to do this on the redirect. Fortunately, you can use Page.IsPostBack to determine whether the page is posting back to itself or not. If the page is posting back, you don't want to do anything; if it is coming from PostOtherForm, you want to load the values. That's what this next code fragment does:


if   (! Page.IsPostBack)


  txtLastName.Text = Session["LastName"].ToString() ;

  txtFirstName.Text = Session["FirstName"].ToString();


lblReferrer.Text  = Session["LastName"].ToString();


The last line of the previous code sets the label to the session variable. That way you can change the textboxes and submit PostSelfForm to see that the textboxes change but the label doesn't.


This works, but it's a bit cumbersome. Every time you add a new textbox, checkbox, or radio button to your form, you need to modify the code or you'll miss passing variables to the receiving form. Wouldn't it be better to have the form do the work? Well, that's what you're going to do. In the code download, there's a page named PostOtherForm2.aspx. Figure 1 shows the pertinent part from this page, IterateThroughChildren. This function walks through the controls (and children controls if there are any) and does a quick check. If the control is one that has data to be passed on, you cast it from a Control to the type of control you need, use the UniqueID of the control (its name) to define the name of the session variable, and finally assign the value from the control you cast. The only change in your button click is you call IterateThroughChildren before you redirect.


void IterateThroughChildren(Control parent)


  foreach (Control c in parent.Controls)




    case "System.Web.UI.WebControls.TextBox":

      System.Web.UI.WebControls.TextBox tb;

               tb =   (System.Web.UI.WebControls.TextBox) c;



    case "System.Web.UI.WebControls.CheckBox":

      System.Web.UI.WebControls.CheckBox cb;

    cb = (System.Web.UI.WebControls.CheckBox) c;




  if (c.Controls.Count > 0)




private void cmdButton_Click(object sender, System.EventArgs e)



  Response.Redirect("ProcessOutput.aspx", true);


Figure 1. Iterate through the controls to create session variables.


Now that the data is loaded, you need to process it. ProcessOutput.aspx (available in the code download) has a simple example of this that dumps out the session variables to a formatted table:


private void Page_Load(object sender, System.EventArgs e)


   ltlOutput.Text = "<table><tr><th>Variable</th><th>Value</th></tr>";

   for(int i =0;i<Session.Count;i++)


       ltlOutput.Text += "<tr><td>" + Session.Keys[i].ToString()

           + "</td><td>" + Session[i].ToString() + "</td></tr>";


   ltlOutput.Text += "</table><P>";

   ltlOutput.Text += Session.Count.ToString() + "</P>";



Now, once you've gotten the session variables, you can process them however you'd like.


Keep your ASP.NET questions coming to me at [email protected].


The sample code in this article is available for download.


Josef Finsel is a software developer specializing in .NET and SQL Server with Avanade, the premier global technology integrator for Microsoft solutions in the enterprise. He has published a number of VB, .NET, and SQL Server articles and, when he isn't hanging around the aspnetpro forums, you can find him working on his own take on Shakespeare's classics. He's also author of The Handbook for Reluctant Database Administrators (Apress).





Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.