Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using embedded standard HTML forms with ASP.NET

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>
like image 797
Rosstified Avatar asked Feb 26 '09 03:02

Rosstified


People also ask

How do I create a simple form in HTML?

Creating a Simple HTML Form 1 Create a new website. 2 In the root folder, create a web page named Form.cshtml and enter the following markup: HTML <!DOCTYPE html> <html> <head> <title>Customer Form</title> </head> <body> <form method="post" > <fieldset> <legend>Add Customer</legend> ... 3 Launch the page in your browser. ...

How to have two forms in ASP NET page?

You can not have two forms inside an asp.net page and both work on code behind. The alternative that you have. Do not set second form, just read the input data that you interesting on code behind.

How to create ASPnet core forms in Visual Studio Code?

This Template is available under Visual C# -> .NET Core option. Name the project as ASPNetCoreForms. In the next screen select .Net Core and ASP.NET Core 2.0 and select an Empty template and Click ok to create the Project. Build and the project.

What is HTML-encodes in ASP NET Web pages?

To help prevent these problems, ASP.NET Web Pages automatically HTML-encodes any text content that you output from your code. For example, when you display the content of a variable or an expression using code such as @MyVar, ASP.NET Web Pages automatically encodes the output. Users make mistakes.


1 Answers

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();
}
like image 93
brendan Avatar answered Sep 19 '22 23:09

brendan