C# 将 ASP.Net 表单数据发布到另一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11149282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post ASP.Net Form data to another page
提问by william
I have an ASP.Net Page, aspxwith its default form.
我有一个ASP.Net Page, aspx默认形式。
I have a SubmitButton for it. Upon clicking, it will post the data to itself. In other words, Button Click Event()from code behind will execute the necessary.
我有一个Submit按钮。单击后,它会将数据发布给自己。换句话说,Button Click Event()从后面的代码会执行必要的。
After that, I would like to post the same data to another ASp.Net Page, aspxfrom another domain.
之后,我想将相同的数据ASp.Net Page, aspx从另一个域发布到另一个域。
So, how can I do it?
那么,我该怎么做呢?
I tried creating a Formin Button Click Eventand a javascriptto Submitthe Formso that it will post. But the Form is not appearing hence there is already aForm` on the page.
我试图创建一个Form在Button Click Event和javascript到Submit了Form,这样就会发布。但是Form is not appearing hence there is already a页面上的 Form`。
Is there anyway to do it?
有没有办法做到这一点?
采纳答案by Chris Gessler
Use the Button's PostBackUrl property. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx
使用按钮的 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>
回答by fenix2222
In code behind just use
在后面的代码中只需使用
Response.Redirect("YourOtherPage.aspx?param1=xxx")
回答by codingbiz
This is one approach which I don't really recommend but it will do what you want. It uses javascript to change the url (e.g. to default2.aspx) the form is posted to using the form's action attribute and then repost the form
这是我并不真正推荐的一种方法,但它会做你想做的。它使用 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);
}
The second page should have EnableViewStateMac="false"
第二页应该有 EnableViewStateMac="false"
<%@ Page Language="C#" EnableViewStateMac="false" AutoEventWireup="true"
CodeBehind="default2.aspx.cs" Inherits="CodeGen.default2" %>
Caution:Turn off MAC generation by setting enableViewStateMac=false in the page or web.config.. This isn't recommended, since the MAC helps prevent people from tampering with your viewstate data. But if tampering with viewstate data isn't a concern (& it may not be for some applications where there's no risk of fraud or security breaches), you can turn it off. Read More
注意:通过在页面或 web.config 中设置 enableViewStateMac=false 来关闭 MAC 生成。不推荐这样做,因为 MAC 有助于防止人们篡改您的视图状态数据。但是,如果不担心篡改视图状态数据(并且对于某些没有欺诈或安全漏洞风险的应用程序可能不是这样),您可以将其关闭。阅读更多
回答by Suamere
I had a similar issue. I had an asp:button which simply performed a postback. In the Page_Load IsPostBack portion, I did some complex validation. Most people just submit the form to the next page and do validation there, then redirect back if it fails. But I thought was was sloppy. So the solution was postback, then upon validation, submit from within the CodeBehind. I believe that's what you're looking for.
我有一个类似的问题。我有一个 asp:button,它只是执行回发。在 Page_Load IsPostBack 部分,我做了一些复杂的验证。大多数人只是将表单提交到下一页并在那里进行验证,然后在失败时重定向回来。但我认为是马虎。所以解决方案是回发,然后在验证后从 CodeBehind 内提交。我相信这就是你要找的。
I'd like to draw this out with "detail", but it's very simple:
我想用“细节”把它画出来,但这很简单:
Server.Transfer("~/folder/page.aspx", True)
The "True" is a flag of whether or not to preserve the POST data. Works fine for me, let me know how it works for you.
“True”是是否保留POST数据的标志。对我来说效果很好,让我知道它对你的效果如何。
http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET
http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET
回答by user6307854
Similar issue. What a pain.
类似的问题。多么痛苦。
In the end I've put a form in the master page (this way it isn't inside another form):
最后,我在母版页中放置了一个表单(这样它不在另一个表单中):
<form method="post" id="asp-form-hack" style="display: none;"></form>
Then, in the aspx pages, I use it like this:
然后,在 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>
Please note that this method allows posting to other domains
请注意,此方法允许发布到其他域

