使用kentico中的response.write重定向html表单不起作用

时间:2016-03-17 12:18:12

标签: javascript c# asp.net http-post kentico

我尝试将HTML表单发布到另一个URL,在我的测试中创建了表单:

<html>
<body onload='document.forms["form1"].submit()'>
<form id='form1' method='POST' action='http://localhost:52035/testpage?'>
<input type='hidden' id='name' value='form1' />
<input type='hidden' name='NEW_ITEM["0"] value='1234-ABC'/>
</form>
</body>
</html>

但不发送,

以下是表单build / send的片段,可通过点击按钮访问:

protected void btnSubmitRequest_Click(object sender, EventArgs e)
        {
var hookUrl = SessionHelper.GetValue("hookurl").ToString();
string formId = "form1";
StringBuilder htmlForm = new StringBuilder();
htmlForm.AppendLine("<html>");
htmlForm.AppendLine(String.Format("<body onload='document.forms[\"{0}\"].submit()'>", formId));
htmlForm.AppendLine(String.Format("<form id='{0}' method='POST' action='{1}'>", formId, hookUrl));
htmlForm.AppendLine("<input type='hidden' id='name' value='form1' />");
// test values 
prod.ProductCode = "1234-ABC"
int i = 0;

htmlForm.AppendLine(String.Format("<input type='hidden' name='NEW_ITEM[\"{0}\"] value='{1}'/>",i, prod.ProductCode));
htmlForm.AppendLine("</form>");
                        htmlForm.AppendLine("</body>");
                        htmlForm.AppendLine("</html>");

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(htmlForm.ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();

}

2 个答案:

答案 0 :(得分:0)

  • 如果你只是搜索它们,已经有很多这个问题了
  • 你错过了关于NEW_ITEM的结束报价......
  • 并且第3次使用Ajax + FormData而不是搞乱html。

以下是您问题的可能解决方案

<body>
  <form id='form1' name='form1' method='POST' action='http://localhost'>
    <input type='text' id='name' value='form1'>
    <input type='text' name='NEW_ITEM["0"]' value='1234-ABC'>
  </form> 
  <script>document.forms.form1.submit()</script>
</body>

答案 1 :(得分:0)

我没有提到我的代码在ascx页面中,我将代码移到了aspx页面,而response.write现在将表单数据发送到外部URL。