表单提交页面刷新两次

时间:2016-01-11 12:18:06

标签: c# forms web-applications submit

我想将数据发布到另一个页面。

问题:  页面刷新两次。 (我认为一次在当前页面中,一次在新页面中)

以下是代码:

protected void btnReturn_Click(object sender, EventArgs e)
   string URI="test";
   var data = new System.Collections.Specialized.NameValueCollection();
   data.Add("code", "1");
   string formID = "formName";
   StringBuilder strForm = new StringBuilder();
   strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\">");

   foreach (string key in data)
      strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">");            

   strForm.Append("</form>");

   StringBuilder strScript = new StringBuilder();
   strScript.Append("<script language='javascript'>");
   strScript.Append("var v" + formID + " = document." + formID + ";");
   strScript.Append("v" + formID + ".submit();");
   strScript.Append("</script>");

   Page.Controls.Add(new System.Web.UI.LiteralControl(strForm.ToString() + strScript.ToString()));

1 个答案:

答案 0 :(得分:0)

使用

Response.Write(strForm.ToString() + strScript.ToString());

而不是

Page.Controls.Add(new System.Web.UI.LiteralControl(strForm.ToString() + strScript.ToString()));
祝你好运;)