C# 在asp.net中的ASPX页面上创建表单
我正在尝试在asp.net应用程序的aspx页面上创建一个带有帖子的表单C# 在asp.net中的ASPX页面上创建表单,c#,asp.net,forms,C#,Asp.net,Forms,我正在尝试在asp.net应用程序的aspx页面上创建一个带有帖子的表单 <form method="POST" action="https://www.vcs.co.za/vvonline/vcs.aspx"> <input type="hidden" id="vcsTerminalId" name="p1" value="a" runat="server"/> <input type="hidden" id="vcsReference" name
<form method="POST" action="https://www.vcs.co.za/vvonline/vcs.aspx">
<input type="hidden" id="vcsTerminalId" name="p1" value="a" runat="server"/>
<input type="hidden" id="vcsReference" name="p2" value="b" runat="server"/>
<input type="hidden" id="vcsDescription" name="p3" value="c" runat="server"/>
<input type="hidden" id="vcsAmount" name="p4" value="d" runat="server"/>
<input type="hidden" id="vcsHash" name="hash" value="q" runat="server"/>
<input type="submit" value="Proceed to payment" />
</form>
然而,在运行时,此表单将被取消,并提交,但用于页面表单。在运行时,我的整个页面被放入一个表单中。我认为这是一个asp的东西
在“我的页面”上工作时,将显示如下内容:
<%@ Page Title="" Language="C#" MasterPageFile="~/template/site.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainContainer" Runat="Server">
//Content inside!!!!!
</asp:Content>
//里面的内容!!!!!
在运行时:
<form method="post" action="SearchResult.aspx?id=1561901" id="form1">
//Content in side
</form>
//侧边内容
我怎样才能添加最上面提到的表单呢?使用WebForms进行跨页面发布总是有点笨拙 为了使我的标记不受黑客攻击,我一直在使用一个帮助器类从CodeBehind执行此操作:
RemotePost remotePostHelper = new RemotePost("https://www.vcs.co.za/vvonline/vcs.aspx");
remotePostHelper.Add("p1", "a");
remotePostHelper.Add("p2", "b");
remotePostHelper.Add("p3", "c");
remotePostHelper.Post();
助手类:
public partial class RemotePost
{
/// <summary>
/// Gets or sets the remote URL to POST to.
/// </summary>
public string PostUrl
{ get; set; }
/// <summary>
/// Gets or sets the form's HTML name.
/// </summary>
public string FormName
{ get; set; }
/// <summary>
/// Gets the collection of POST data.
/// </summary>
public NameValueCollection PostData
{ get; private set; }
/// <param name="postUrl">The remote URL to POST to.</param>
public RemotePost(string postUrl)
{
this.PostData = new NameValueCollection();
this.PostUrl = postUrl;
this.FormName = "formName";
}
/// <summary>
/// Adds the specified name and value to the POST data collection..
/// </summary>
/// <param name="name">The name of the element to add</param>
/// <param name="value">The value of the element to add.</param>
public void Add(string name, string value)
{
this.PostData.Add(name, value);
}
public void Post()
{
var context = HttpContext.Current;
context.Response.Clear();
context.Response.Write("<html><head>");
context.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">", this.FormName));
context.Response.Write(string.Format("<form name=\"{0}\" method=\"post\" action=\"{1}\" >", this.FormName, this.PostUrl));
foreach(string name in this.PostData)
{
context.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", HttpUtility.HtmlEncode(name), HttpUtility.HtmlEncode(this.PostData[name])));
}
context.Response.Write("</form>");
context.Response.Write("</body></html>");
context.Response.End();
}
}
公共部分类RemotePost
{
///
///获取或设置要发布到的远程URL。
///
公共弦姿势
{get;set;}
///
///获取或设置窗体的HTML名称。
///
公共字符串FormName
{get;set;}
///
///获取POST数据的集合。
///
public NameValueCollection PostData
{get;私有集;}
///要发布到的远程URL。
公共RemotePost(字符串姿势)
{
this.PostData=新的NameValueCollection();
this.postrl=postrl;
this.FormName=“FormName”;
}
///
///将指定的名称和值添加到POST数据集合。。
///
///要添加的元素的名称
///要添加的元素的值。
公共void添加(字符串名称、字符串值)
{
this.PostData.Add(名称、值);
}
公共空缺职位()
{
var context=HttpContext.Current;
context.Response.Clear();
context.Response.Write(“”);
Write(string.Format(“,this.FormName));
Write(string.Format(“,this.FormName,this.postrl));
foreach(this.PostData中的字符串名称)
{
context.Response.Write(string.Format(“,HttpUtility.HtmlEncode(name),HttpUtility.HtmlEncode(this.PostData[name]));
}
context.Response.Write(“”);
context.Response.Write(“”);
context.Response.End();
}
}
任何你想在“mainContainer”中写的东西都会放在母版页的正文中。@ShujaatSiddiqui是的,这就是我显示页面的地方,在这个页面上我想要一个按钮来发布文章。所以我需要它的形式。但主容器中的所有内容都已放置在表单中,据我所知,您无法嵌套表单,因此它删除了我的表单,并只使用了sumbit按钮。WebForms
的奇妙之处在于,您已经有了一个表单
。您尝试添加的任何其他表单
都将被嵌套,这是无效的。或者您可以去掉主表单
或使用它。你决定吧。既然我不能在表单中添加新表单,也不能在contentPlaceHolder中添加操作,有没有办法在重定向中重新创建post方法?表单提交如何处理其参数?类似于:Response.Redirect(“?”+“p1=005S&p2=“+Reference+”&p3=“+Description+”&p4=“+Amount+”&hash=“+hash”)@很高兴我能帮忙:)