简体   繁体   English

使用嵌入式标准 HTML forms 与 ASP.NET

[英]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.我有一个标准的 aspx 页面,我需要在其中添加另一个标准 HTML 表单并将其提交到另一个位置(外部站点),但是每当我按下提交按钮时,该页面似乎都会回发而不是使用子形成动作 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>

It's an interesting problem.这是一个有趣的问题。 Ideally you only want the 1 form tag on the page as other users have mentioned.理想情况下,您只需要其他用户提到的页面上的 1 个表单标签。 Potentially you could post the data via javascript without having 2 form tags.您可能可以通过 javascript 发布数据,而无需 2 个表单标签。

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.不是 100% 确定这是否对您有用,但我认为这就是您必须采用的方法。

<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.如果 javascript 不是一个可行的选项 - 您可以使用.Net 的 HttpWebRequest object 在后面的代码中创建 post 调用。 Would look something like this in the code behind (assuming your text field is an asp textbox:在后面的代码中看起来像这样(假设您的文本字段是一个 asp 文本框:

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();
}

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.如果您将 ASP.NET 按钮添加到表单,并将其 PostBackUrl 属性设置为外部站点,则所有表单数据都将发布到该 URL。

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.您可以在<form> form> 之前插入一个</form>标签来关闭导致问题的 asp.net 表单。 Do not forget to add a <form> tag after your html form.不要忘记在 html 表单之后添加<form>标签。 It may cause the editor to give you an exception, but do not worry, it will work.它可能会导致编辑器给你一个异常,但不要担心,它会起作用的。

Nested forms are not possible in HTML according to the W3C .根据 W3C ,在 HTML 中嵌套 forms 是不可能的。 You can achieve your intended result using JavaScript or with jQuery as explained by Peter on a blog called My Thoughts.您可以使用 JavaScript 或jQuery来实现您的预期结果,正如 Peter 在名为 My Thoughts 的博客中所解释的那样。

In my experience, Appetere Web Solutions has the best solution.以我的经验,Appetere Web Solutions 有最好的解决方案。 Simple and elegant...and it's not a hack.简单而优雅......而且它不是一个黑客。 Use the PostBackUrl.使用 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.我不想使用 Javascript 因为我不想在我的母版页中包含使用它的每个页面。

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. Cowgod 的回答让我想到了 my.aspx 页面中嵌套的 forms 。 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.我最终从 main.aspx 表单(

Suddenly, everything was submitting as it was supposed to.突然间,一切都按原样提交。 Is this a hack?这是黑客吗?

ASP.NET allows you to have multiple forms on one page, but only one can be runat=server . ASP.NET 允许您在一页上拥有多个 forms ,但只能运行一个runat=server However I don't think you can nest forms at all.但是我认为你根本不能嵌套 forms 。

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.这不是一个好的解决方案,除非您可以将表单放在母版页的表单之外,并使用 javascript 提交第二个表单,但这也好不到哪里去。 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?我认为您不能从代码隐藏中进行 POST 调用,可以吗? I'm sure there's some way.我敢肯定有什么办法。 But that's probably the only solution: from code.但这可能是唯一的解决方案:来自代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM