gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:04:01 31 4
gpt4 key购买 nike

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

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com