views:

274

answers:

4

I am using ASP.NET 3.5.

I have a button called btnSubmit and on this button in the PostBackURL i have a URL the page must submit data to.

<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="Hand" 
        PostBackUrl="http://now.eloqua.com/e/f2.aspx" />

Now when i have this i cant excecute more code in the button click event like so.....

Protected Sub btnSubmit_Click(ByVal sender As Object, 

    Dim name as String
    name = "HELP"

End Sub

Why is this and how can i do a PostBackURL and still excecute code when the button is click?

Note: The Postback URL is located at another compnay who will capture the data

A: 

Can you do this?

<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="Hand" 
        Autopostback="true" />
Protected Sub btnSubmit_Click(ByVal sender As Object, 

    Dim name as String
    name = "HELP"
    Response.Redirect("http://now.eloqua.com/e/f2.aspx")
End Sub
andrewWinn
Nope it does not work as the URL needs cant be used as Server.Transfer(Located ot another company) or Repose.Redirect(data is not transfered then)
Etienne
A: 

Could you just do the manipulation you need to do in Javascript before initiating the postback to the other server?

Otherwise you can't set the PostBackURL if you want the page to post back to your server first so that you can run your code. You might be able to force a second postback after your code has run by doing something like this

Response.Write("document.forms[0].submit();");

Provided that the form knows the correct URL to postback to in that case.

bwarner
No no no no... :| Do not do this. Use RegisterStartupScript and GetPostBackEventReference for this sort of thing.
Bryan
However... the idea is valid. I will edit my answer.
Bryan
+1  A: 

You need to tackle this in a different way. The PostBackUrl property means that your server doesn't get any notice from the user's browser that it's now going to a new URL, so there is no way to get an event to fire.

Two options:

1) Add an OnClientClick handler with an AJAX call to do some server processing.

2) Capture the data you need in a local postback and send it to the new server another way via Request.Redirect or Server.Transer.

3) Like bwarner suggests, you can have your page postback to your own server, then cause another postback automatically. First remove the PostBackUrl in the aspx. Then add something like this in your button handler:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    name = "HELP";  // your code here
    btnSubmit.PostBackUrl = "http://newserver/page.aspx";
    ClientScript.RegisterStartupScript(this.GetType(), "autopostback", ClientScript.GetPostBackEventReference(btnSubmit, ""));
}

Although... To be honest, I'm not entirely sure GetPostBackEventReference honors the PostBackUrl property. Let us know if you figure that out. :)

Bryan
Nope, this did not work for me.........
Etienne
So GetPostBackEventReference generates the same code whether or not there is a PostBackUrl set? I think that would qualify as a bug.
Bryan
A: 

What about not using any client side code and just building the request server side. You would have to manually build up your post data but I think it would work:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
name = "HELP";  // your code here

//form submit code

//build all your fields into post data
string field1 = Field1.Text;
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="field1="+field1 ;
byte[]  data = encoding.GetBytes(postData);

// Prepare web request...
HttpWebRequest myRequest =
  (HttpWebRequest)WebRequest.Create("http://now.eloqua.com/e/f2.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
}
brendan