title: Override Asp Form Action URL Dynamically
body: <p>I am having problem with overriding Form Action URL </p>
  
  <p>I have a class to override the Form Action.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
         &lt;form id="form1" runat="Server"  action="https://www.test.com"&gt;
          &lt;div style=""&gt;
              &lt;asp:button runat="Server" onclick="PostMe" Text="Post"/&gt;
          &lt;/div&gt;
  
          &lt;asp:HiddenField runat="server"  ID="test" value="0" /&gt;
          &lt;asp:HiddenField runat="server"  ID="test2" value="0" /&gt;
  
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  
  
  
  
  &lt;script runat="server"&gt;
  
      void PostMe(Object sender,EventArgs e){
  
          RemotePost2 myremotepost =  new RemotePost2();
          myremotepost.Url = "https://www.test.com";
          myremotepost.Add("field1","Tom");
          myremotepost.Add("field2","Sawyer");
          myremotepost.Post();
      }
  
  
      public class RemotePost2{
              private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection();
  
  
              public string Url = "";
              public string Method = "post";
              public string FormName = "forms";
  
              public void Add(string name,string value){
                  Inputs.Add(name,value);
              }
  
              public void Post(){
                  System.Web.HttpContext.Current.Response.Clear();
  
                  System.Web.HttpContext.Current.Response.Write("&lt;html&gt;&lt;head&gt;");
                  System.Web.HttpContext.Current.Response.Write(string.Format("&lt;/head&gt;&lt;body onload=\"document.{0}.submit()\"&gt;",FormName));
                  System.Web.HttpContext.Current.Response.Write(string.Format("&lt;form name=\"{0}\" method=\"{1}\" action=\"{2}\" &gt;",FormName,Method,Url));
                  for(int i=0;i&lt; Inputs.Keys.Count;i++){
                      System.Web.HttpContext.Current.Response.Write(string.Format("&lt;input name=\"{0}\" type=\"hidden\" value=\"{1}\"&gt;",Inputs.Keys[i],Inputs[Inputs.Keys[i]]));
                  }
                  System.Web.HttpContext.Current.Response.Write("&lt;/form&gt;");
                  System.Web.HttpContext.Current.Response.Write("&lt;/body&gt;&lt;/html&gt;");
  
                  System.Web.HttpContext.Current.Response.End();
              }
      }
  </code></pre>
  
  <p></p>
  
  <p>The problem is I cannot post field1 and field2 instead the form posts test and test2 values. When I use postBackUrl attribute it still posts test and test2. </p>
  
  <p>What I want to do is to post field1 and field2 when the form action is not blank. Because when there is no action it works fine and I cannot modify the action on the code.</p>
  
  <p>Your help will be much appreciated
  Cheers</p>