Publish ASP.Net form data to another page

I have an ASP.Net Page, aspx with its default form.

I have a Submit button. After clicking, it will send the data to itself. In other words, Button Click Event() from the code behind will execute the necessary.

After that, I would like to send the same data to another ASP.Net Page, aspx from another domain.

So how can I do this?

I tried creating a Form in Button Click Event and javascript before Submit Form so that it posted a post. But Form is not appearing hence there is already a Form` on the page.

Is there any way to do this?

+4
source share
5 answers

Use the Button property 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> 
+8
source

This is one approach that I really do not recommend, but it will do what you want. It uses javascript to change the URL (e.g. default2.aspx), the form is submitted to use the form's action attribute, and then the form is submitted

  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: Disable MAC generation by setting enableViewStateMac = false on the page or web.config. This is not recommended as the MAC helps prevent unauthorized use of your data in the view. But if faking data in the form of representations is not a concern (perhaps this may not be for some applications where there is no risk of fraud or security breaches), you can disable it. More details

+2
source

I had a similar problem. I had an asp: button that just did the postback. In the Page_Load IsPostBack part, I performed some complicated validation. Most people simply submit the form to the next page and check it there, and then redirect it if it fails. But I thought it was messy. So the solution was a postback, then after checking, send from CodeBehind. I believe what you are looking for.

I would like to do this with "detail", but it is very simple:

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

True is a flag of whether to save POST data or not. Works great for me, let me know how it works for you.

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

+2
source

A similar question. This is pain.

At the end, I placed the form on the main page (thus, it is not inside another form):

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

Then on 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 you to send messages to other domains.

0
source

In code only use

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

All Articles