【问题标题】:Post ASP.Net Form data to another page将 ASP.Net 表单数据发布到另一个页面
【发布时间】:2012-06-24 08:15:46
【问题描述】:

我有一个默认形式的ASP.Net Page, aspx

我有一个Submit 按钮。点击后,它会将数据发布给自己。换句话说,后面代码中的Button Click Event() 将执行必要的操作。

之后,我想将相同的数据从另一个域发布到另一个ASp.Net Page, aspx

那么,我该怎么做呢?

我尝试在Button Click Event 中创建FormjavascriptSubmit Form,以便它会发布。但是页面上的Form is not appearing hence there is already aForm`。

还有办法吗?

【问题讨论】:

    标签: c# asp.net html forms


    【解决方案1】:

    使用按钮的 PostBackUrl 属性。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx

    <%@ page language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="head1" runat="server">
      <title>Button.PostBackUrl Example</title>
    </head>
    <body>    
      <form id="form1" runat="server">
    
        <h3>Button.PostBackUrl Example</h3>
    
        Enter a value to post:
        <asp:textbox id="TextBox1" 
          runat="Server">
        </asp:textbox>
    
        <br /><br />
    
        <asp:button id="Button1" 
          text="Post back to this page"
          runat="Server">
        </asp:button>
    
        <br /><br />
    
        <asp:button id="Button2"
          text="Post value to another page" 
          postbackurl="Button.PostBackUrlPage2cs.aspx" 
          runat="Server">
        </asp:button>
    
      </form>
    </body>
    </html>
    

    【讨论】:

    • 我看到您使用了 2 个不同的按钮。可以使用&lt;asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="javascript:return ValidateForm()" CssClass="btn" onclick="btnSubmit_Click" PostBackUrl="~/ReceiveForm.aspx" /&gt; 之类的单个按钮来完成吗
    • 第一次,不要填写PostBackUrl,在第一次回发时填写。不确定表单帖子,但常规回发应该可以与您拥有的 JS 代码一起正常工作。
    • 我刚刚发现PostBackUrl 仅适用于同一域中的页面。但我的要求是发布到另一个域。 PostBackURL 还能用吗? Tkz。
    • 没有。它不会发布到另一个域。您必须使用@tcoder 提供的答案
    【解决方案2】:

    在后面的代码中使用

    Response.Redirect("YourOtherPage.aspx?param1=xxx")
    

    【讨论】:

    • 这不会发布页面,但会得到它
    【解决方案3】:

    这是一种我并不真正推荐的方法,但它会做你想做的事。它使用 javascript 来更改表单发布到的 url(例如到 default2.aspx),使用表单的 action 属性,然后重新发布表单

        protected void btnClick(object sender, EventArgs e)
        {
            string script = "<script> document.forms[0].action='default2.aspx'; document.forms[0].submit(); </script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "postform", script);
        }
    

    第二页应该有EnableViewStateMac="false"

    <%@ Page Language="C#" EnableViewStateMac="false" AutoEventWireup="true"
                 CodeBehind="default2.aspx.cs" Inherits="CodeGen.default2" %>
    

    警告:通过在页面或 web.config 中设置 enableViewStateMac=false 来关闭 MAC 生成。不建议这样做,因为 MAC 有助于防止人们篡改您的视图状态数据。但是,如果篡改视图状态数据不是问题(并且对于一些没有欺诈或安全漏洞风险的应用程序可能不是问题),您可以将其关闭。 Read More

    【讨论】:

      【解决方案4】:

      我遇到了类似的问题。我有一个简单的执行回发的 asp:button。在 Page_Load IsPostBack 部分,我做了一些复杂的验证。大多数人只是将表单提交到下一页并在那里进行验证,然后如果失败则重定向回来。但我认为这是马虎。所以解决方案是回发,然后在验证后从 CodeBehind 中提交。我相信这就是您正在寻找的。​​p>

      我想用“细节”把这个画出来,但是很简单:

      Server.Transfer("~/folder/page.aspx", True)
      

      “True”是是否保留 POST 数据的标志。对我来说很好,让我知道它对你有用。

      http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET

      【讨论】:

        【解决方案5】:

        类似的问题。多么痛苦。

        最后我在母版页中放了一个表单(这样它就不会在另一个表单中):

        <form method="post" id="asp-form-hack" style="display: none;"></form>
        

        然后,在 aspx 页面中,我这样使用它:

        <script>
            $(document).ready(function () {
                var $loginButton = $("#login-button");
                var loginForm = document.forms["asp-form-hack"];
                loginForm.action = "<page-to-postback>.aspx";
                $loginButton.on("click", Login);
        
                function Login() {
                    // read values from input fields in the page
                    var username = $("#t_username").val();
                    var password = $("#t_password").val();
        
                    // create input elements with the same values
                    var usernameInput = document.createElement("input");
                    usernameInput.name = "txtUsername";
                    usernameInput.type = "text";
                    usernameInput.value = username;
                    var passwordInput = document.createElement("input");
                    passwordInput.name = "txtPassword";
                    passwordInput.type = "password";
                    passwordInput.value = password;
        
                    // append the input elements to the form
                    loginForm.appendChild(usernameInput);
                    loginForm.appendChild(passwordInput);
        
                    // setTimeout prevents your page from understanding
                    // that you are submitting from another form
                    setTimeout(function () {
                        loginForm.submit();
                    }, 0);
                }
            });
        </script>
        

        请注意,此方法允许发布到其他域

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-16
          • 1970-01-01
          • 1970-01-01
          • 2018-05-26
          相关资源
          最近更新 更多