Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post ASP.Net Form data to another page

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 aForm` on the page.

Is there anyway to do it?

like image 374
william Avatar asked Jun 22 '12 01:06

william


2 Answers

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>
like image 68
Chris Gessler Avatar answered Sep 22 '22 00:09

Chris Gessler


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

like image 36
codingbiz Avatar answered Sep 19 '22 00:09

codingbiz