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?
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With