c# - 在 asp.net 中的 ASPX 页面上创建表单

标签 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>

如何在运行时此表单消失并提交但用于页面表单。在运行时,我的整个页面被放入表单中。我认为这是一个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();
    }
}

关于c# - 在 asp.net 中的 ASPX 页面上创建表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25564109/

相关文章:

c# - 带 or 的 CSharp 开关表达式和带条件的默认保护

c# - 尽管 list<T> 在内部使用数组(固定的),但它如何动态工作?

c# - 无法让我的更新工作

c# - 如何将 List<string> 中的 Uri 转换为 List<Uri>?

javascript - 在 React 中迭代表单元素数组

javascript - 焦点事件监听器未触发不可编辑的输入

javascript - 在我们的表单中使用 javascript 的优缺点是什么?

c# - Asp.Net Core 中的@Html.Action

asp.net - SQL查询查找车站之间的火车

c# - 如何从 C# 中的 HTML 页面中提取关键字?