在asp.net的ASPX页面上创建一个表单

时间:2014-08-29 08:05:50

标签: 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="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>

在运行时期间,此表单如何消失和提交但用于页面表单。在运行期间,我的整个页面都放在一个表单中。我认为这是一件非常棒的事情。

在我的页面上工作时,它看起来像这样:

<%@ 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>

我如何添加并且是最常提到的表格?

1 个答案:

答案 0 :(得分:3)

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