Using embedded standard HTML forms with ASP.NET

前端 未结 7 1993
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 11:54

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 sub

相关标签:
7条回答
  • 2020-12-14 12:23

    In my experience, Appetere Web Solutions has the best solution. Simple and elegant...and it's not a hack. Use the PostBackUrl.

    I just tried it and everything works as expected. I didn't want to use Javascript because I didn't want to include it in my Master Page for every page that uses it.

    0 讨论(0)
  • 2020-12-14 12:28

    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?

    0 讨论(0)
  • 2020-12-14 12:31

    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.

    0 讨论(0)
  • 2020-12-14 12:31

    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.

    0 讨论(0)
  • 2020-12-14 12:36

    There is a very nice tricky solution for this problem.

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

    0 讨论(0)
  • 2020-12-14 12:37

    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();
    }
    
    0 讨论(0)
提交回复
热议问题