title: Automatically post form to another URL
body: <p>I'm working with a payment page that requires me to post form data to it. I know how to post my form to a different page using a button's PostBackUrl property, and I know how to automatically submit a form so it posts back to itself onload, but I can't figure out how to combine the two.</p>
  
  <p>I have a form that in OnLoad sets the values of a bunch of hidden fields to post to the payment page. When it loads, I want it to automatically post to the payment page without the user having to click a button. If I set the Form's Action property in Page_Load, I get a "Validation of viewstate MAC failed" error. I've seen this before when modifying the contents of a server control client-side then posting back. But here I've set the Action server-side and I'm not even posting back to the page so how can does it validate the viewstate? </p>
  
  <p>I've just discovered that if I place a Button on the page with the same PostBackUrl as I set the Action to, this error goes away and it works, so I guess that's a work around, but can anyone explain the error and/or suggest a better way of auto posting the form to a different URL when the page loads?</p>
  
  <p>EDIT:
  Here is my testing code as I currently have it:</p>
  
  <p>Intermediate page:</p>
  
  <pre><code>&lt;form id="form1" runat="server"&gt;
  &lt;asp:Repeater ID="rptPostData" runat="server"&gt;
      &lt;ItemTemplate&gt;
          &lt;input type="hidden" name="&lt;%# Eval("key") %&gt;" value="&lt;%# Eval("value") %&gt;" /&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:Repeater&gt;
  &lt;asp:Button ID="Button1" runat="server" Text="" 
      PostBackUrl="http://localhost:64545/Payment.aspx" Style="visibility:hidden"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack) //(Should never post back)
          {
              //this.form1.Method = "post";
              this.form1.Action = "http://localhost:64545/Payment.aspx";
  
              Dictionary&lt;string, string&gt; postData = new Dictionary&lt;string, string&gt;();
              postData.Add(_accountIDKey, "555555");
              postData.Add(_amountKey, "125.75");
              postData.Add(_referenceIDKey, Guid.NewGuid().ToString());
              postData.Add(_accountDataKey, "{'FirstName':'John','LastName':'Sleevenstein','Address1':'123 Fake St.'}");
              postData.Add(_returnUrlKey, Request.RawUrl);    //This should be a receipt page
              postData.Add(_hashKey, CreateHash(postData));
  
              rptPostData.DataSource = postData;
              rptPostData.DataBind();
  
              ScriptManager.RegisterStartupScript(this, typeof(WebForm1), "PostScript", "document.form1.submit();", true);
          }
      }
  </code></pre>