开发者

Manipulating Form.Action

开发者 https://www.devze.com 2023-04-07 03:08 出处:网络
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.

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 ba开发者_运维问答ck 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.


If the page does not need to do validation/processing on server side when user clicks the button then you can do it like,

In ASPX:

<asp:Button ID="btnSubmit" runat="server"
   OnClientClick="$('form').get(0).setAttribute('action', 'ScriptActionUrl'); $('form').submit();"
   Text="Submit">
</asp:Button>

If there is something to be processed/validated on server-side then do it like,

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);
    }
}


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.


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.


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:

  1. Create an asmx web service project; Add one webmethod called SayHello. It will accept one argument - Username and return Hello <Username> to the calling client.
  2. Run this web service. Assume its url is something like http://localhost:6107/DemoAsmx/demo.asmx
  3. Open another VS instance. Create a normal web project. Add a aspx page. Add one textbox. Set its id to Username
  4. Add a button to the page. Set it PostbackUrl property to http://localhost:6107/DemoAsmx/demo.asmx/SayHello
  5. Run the web project. Now Enter a name in the textbox, and click the button. You will see the xml response from the asmx web service.

This can be done via javascript also. I hope this helps.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消