tags:

views:

254

answers:

4

I have a button called btnSubmit where i set the Form action attribute to a URL like so.

 Protected Sub btnSubmit_Click(ByVa....

    Form.Attributes.Add("action", "http://now.eloqua.com/e/f2.aspx")

End Sub

This does work but for some reason it only works after I clicked the button the second time. Why is this and how can i fix this?

I am using ASP.NET 3.5 with VB.NET(C# code is also fine)

What I actually want to do it do some code on the submit and then as soons as everything is complete, then set the form action attribute where it must send the form data to another location at that URL.

Thanks in advanced!

+1  A: 

It doesn't work the first time because the form on which the attributes are added is already rendered.

Fabian Vilers
A: 

The first time you click the button, it sets the form attribute, the second time you click it, it's submitting the form that you edited the first time round...

You might want to set the form attribute at some other point in the page lifecycle.

Paddy
A: 

If you need to retain the POST data between pages you might want to use Server.Tranfer. See here for a most excellent explanation: http://stackoverflow.com/questions/1482470/using-asp-net-how-do-i-redirect-a-user-and-change-the-post-data-at-the-same-time

edosoft
+2  A: 

Fabian is right.

Your code executes on the serverside, after the first submit.

To do what you want, you'll need to emit some javascript using the scriptmanager, which executes in the client, since it will all have to happen before the submit happens in the first place.

Use Page.ClientScript.RegisterClientScriptBlock() to emit a script block with a suitable function which does your stuff, then sets the form's action attribute. Call that function from the button using the OnClientClick attribute.

Tor Haugen