Form action attribute not working - have to click submit button twice
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 advance!
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.
It doesn't work the first time because the form on which the attributes are added is already rendered.
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.
If you need to retain the POST data between pages you might want to use Server.Tranfer. See here for a most excellent explanation: Using asp.net, how do I redirect a user and change the POST data at the same time?
精彩评论