Reputation: 18800
I have to use Form.Action to redirect to a script that will be picking up values from my page. It is worth noting that this script is external.
My issue is that I also want the button that is clicked and has the Action hooked up to it, to also complete some functionality in the code behind first.
Is there anyway I can either :
In the buttons click event handler, can I set the Form.Action
and then call something such as Form.Submit
?
OR
Set up the Form.Action
in advance and then somehow have the button posting back before the action takes place.
If this is not possible, any pointers in the correct direction with how I can achieve this would be appreciated.
Upvotes: 2
Views: 727
Reputation: 7249
In ASPX:
<asp:Button ID="btnSubmit" runat="server"
OnClientClick="$('form').get(0).setAttribute('action', 'ScriptActionUrl'); $('form').submit();"
Text="Submit">
</asp:Button>
In ASPX:
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit">
</asp:Button>
In Page-behind-code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Page.Validate(); //Do Validation or some other processing
if(Page.IsValid)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "$('form').get(0).setAttribute('action', 'ScriptActionUrl'); $('form').submit();", true);
}
}
Upvotes: 3
Reputation: 5402
Since the page needs to submit to the page not under your control I would recommend creating a web service tied to the button click. Using code like the following you can create javascript events that occur before the page submits:
btnSubmit.Attributes.Add("onclick", "javascript:functionName()")
Replace functionName with the name of the function you are making your web service call in. This has the added ability to cancel the form's submission based upon the web service's results by doing:
return false;
Upvotes: 0
Reputation: 11985
This is quite possible by setting the PostBackUrl property for any button control (like asp.net button or link button). For a proof of concept do the following:
Hello <Username>
to the calling client.http://localhost:6107/DemoAsmx/demo.asmx
Username
PostbackUrl
property to http://localhost:6107/DemoAsmx/demo.asmx/SayHello
This can be done via javascript also. I hope this helps.
Upvotes: 0
Reputation: 5402
Is this a form that you care what the submitted form does? If all that must be done is for it to submit you can build the POST submitted data inside your button click event and send it off without ever redirecting the user and continue processing your code.
There is useful documentation on one way to do it here though there are several ways to do it.
Typically you would build your POST data with a builder then send it off. Here is an example:
// Create a new WebClient instance to send the data with
WebClient myWebClient = new WebClient();
// Collection to hold our field data
NameValueCollection postValues = new NameValueCollection();
// Add necessary parameter/value pairs to the name/value container.
postValues.Add("LoginName", tbLoginName.Text);
postValues.Add("Password", tbPassword.Text);
// Send off the message
byte[] responseArray = myWebClient.UploadValues("http://website.com", postValues);
// Decode and display the response.
tbResponse.Text = Encoding.ASCII.GetString(responseArray);
This code is just a mock up but it should point you in the right direction if this is the path you want to take.
Upvotes: 1
Reputation: 17680
You could do what you need to do in the code behind and then call Server.Transfer(string url,bool preserveFormVariables); to move the request to another page to handle.
You can then use Request.Form at the other page to get out the values you need.
Upvotes: 0