views:

2113

answers:

6

I have a standard aspx page with which I need to add another standard HTML form into and have it submit to another location (external site), however whenever I press the submit button the page seems to do a post back rather than using the sub-forms action url.

A mock up of what the form relationships is below. Note in the real deployment the form will be part of a content area of a master page layout, so the form needs to submit independantly from the master page form.

    <html xmlns="http://www.w3.org/1999/xhtml" >
       <head runat="server">
          <title>Untitled Page</title>
       </head>
       <body>
           <form id="form1" runat="server">
           <div>
               <form id="subscribe_form" method="post" action="https://someothersite.com" name="em_subscribe_form" > 
                    <input type="text" id="field1" name="field1" />
                    <input id="submitsubform" type="submit" value="Submit" />
               </form>
           </div>
           </form>
       </body>
   </html>
A: 

ASP.NET allows you to have multiple forms on one page, but only one can be runat=server. However I don't think you can nest forms at all.

You might have to make a new master page, one without a form tag on it so the form will work on that one page only. This is not a good solution, unless you can place the form outside the master pages' form, and use javascript to submit the second form, but that's hardly better. There really is no good solution for what you are trying to achieve, and if so I'd like to hear it. I don't think you can do a POST call from a code-behind, can you? I'm sure there's some way. But that's probably the only solution: from code.

goldenratio
You can do a POST in code behind, see my example.
brendan
+2  A: 

Nested forms are not possible in HTML according to the W3C. You can achieve your intended result using JavaScript or with jQuery as explained by Peter on a blog called My Thoughts.

cowgod
+4  A: 

It's an interesting problem. Ideally you only want the 1 form tag on the page as other users have mentioned. Potentially you could post the data via javascript without having 2 form tags.

Example taken from here, modified for your needs. Not 100% sure if this will work for you but I think this is how you'll have to approach it.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">

function postdata()
{
   var fieldValue = document.getElementById("field1").value;
   postwith("http://someothersite.com",{field1:fieldValue});
}

function postwith (to,p) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
           <div>
                <input type="text" id="field1" name="field1" />
                <asp:Button ID="btnSubmitSubscribe" runat="server" Text="Submit" OnClientClick="postdata(); return false;" />

       </div>

</div>
</form>
</body>
</html>

If javascript is not a viable option - you can use .Net's HttpWebRequest object to create the post call in code behind. Would look something like this in the code behind (assuming your text field is an asp textbox:

private void OnSubscribeClick(object sender, System.EventArgs e)
{
string field1 = Field1.Text;


ASCIIEncoding encoding=new ASCIIEncoding();
string postData="field1="+field1 ;
byte[]  data = encoding.GetBytes(postData);

// Prepare web request...
HttpWebRequest myRequest =
  (HttpWebRequest)WebRequest.Create("http://someotherwebsite/");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}
brendan
Javascript solutions are great if the user has javascript enabled...
goldenratio
That's crafty...
Chuck Conway
added a javascript-less method as well
brendan
Ah, nice. I knew there was a way to do it from code-behind. Thanks.
goldenratio
Works beautifully. Thanks for this!
RM
A: 

I had the same situation as Ross - except that my input types were all of the "hidden" varitey.

Cowgod's answer got me thinking about nested forms within my .aspx page. I ended up "un-nesting" my 2nd form OUT of the main .aspx form ( ) and placed it (along with my js script tags) just under the body tag - but before the main .aspx form tag.

Suddenly, everything was submitting as it was supposed to. Is this a hack?

Doug
+1  A: 

there is a very nice tricky soloution for this problem.

you can insert a < /form > tang before your < form > to close the asp.net form which couses the problem. fo not forget to add a tag after your html form. It may couse the editor to give you an exception. do not worry, it will work.

hacky, but this worked.
James Lawruk
A: 

If you add an ASP.NET button to the form, and set its PostBackUrl property to the external site, then all the form data will be posted to that URL.

Appetere Web Solutions