Reputation: 7704
I have an ASP.Net Page, aspx
with its default form.
I have a Submit
Button for it. Upon clicking, it will post the data to itself. In other words, Button Click Event()
from code behind will execute the necessary.
After that, I would like to post the same data to another ASp.Net Page, aspx
from another domain.
So, how can I do it?
I tried creating a Form
in Button Click Event
and a javascript
to Submit
the Form
so that it will post. But the Form is not appearing hence there is already a
Form` on the page.
Is there anyway to do it?
Upvotes: 8
Views: 39759
Reputation: 129
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:
<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
Upvotes: 1
Reputation: 6258
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.
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.
http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET
Upvotes: 2
Reputation: 26396
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
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"
<%@ 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
Upvotes: 2
Reputation: 4730
In code behind just use
Response.Redirect("YourOtherPage.aspx?param1=xxx")
Upvotes: -3
Reputation: 23123
Use the Button's PostBackUrl property. 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>
Upvotes: 12