繁体   English   中英

在ASP.NET的ASPX页面上创建表单

[英]Create a form on an ASPX page in asp.net

我正在尝试在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>

我该如何添加,该表格是最上面提到的吗?

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

暂无
暂无

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

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