2014-08-29 133 views
2

我想在我的asp.net應用程序的aspx頁面上創建一個表單。在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> 

如何添加,並提到的Form頂部?

+0

任何你會寫在「mainContainer」中的東西都會放在主頁面的主體中。 – 2014-08-29 08:09:55

+1

@ShujaatSiddiqui是的,這就是我顯示我的頁面,在這個頁面上我想要一個按鈕來發布信息。所以我需要一個表單。但是,Main Container中的所有內容都已經放置在一個表單中,並且就我所知,您無法嵌套表單,因此它將刪除我的表單並僅包含sumbit按鈕。 – Pomster 2014-08-29 08:11:49

+0

我不會讓你的問題變得非常簡單。只需從工具箱中拖動一個按鈕。在主容器內部,您可以使用按鈕來發布表單。 – 2014-08-29 08:14:37

回答

3

使用WebForms進行跨頁面發佈一直有點尷尬。

爲了讓我的標記從黑客乾淨,我一直在使用一個輔助類從代碼隱藏做到這一點:

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(); 

Helper類:

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(); 
    } 
} 
+0

你先生是我的英雄! – Pomster 2014-08-29 09:59:22

+0

@Pomster很高興我能幫忙:) – 2014-08-29 11:48:40

相關問題