1

I'm using webforms and I need to create and POST an http form to a payment gateway for one of my clients. I'm using a user control that is displayed in a masterpage, however they already have an existing <form> on the page, so I'm pretty sure I cannot add another.

What is the best practice method to create and post a form in C# from the code behind? Currently I have the following:

var nvc = new System.Collections.Specialized.NameValueCollection
    {
        {"EWAY_ACCESSCODE", ewayResponse.AccessCode},
        {"EWAY_CARDNAME", txtCcName.Text},
        {"EWAY_CARDNUMBER", txtCcNumber.Text},
        {"EWAY_CARDEXPIRYMONTH", ddlCcExpMonth.SelectedValue},
        {"EWAY_CARDEXPIRYYEAR", ddlCcExpYear.SelectedValue},
        {"EWAY_CARDCVN", txtCcv.Text}
    };

    var client = new WebClient();
    client.UploadValues(ewayResponse.FormActionURL, nvc);

This does work, but is there a better / alternative way to do it?

1

2 Answers 2

4

Aside from the previous suggestion:

  • why wouldn't a (simpler) Button.PostbackUrl work?

    <asp:Button ID="externalPost" runat="server" PostBackUrl="http://www.somewhere.com/" Text="Post Somewhere" />
    

Also, assuming that's a payment gateway you're referring to, you're doing a server-side POST, which has implications on PCI Compliance (vs. a direct form post from client/browser).

Hth.

Sign up to request clarification or add additional context in comments.

3 Comments

I agree with the PCI stuff, that's why I'd like to do find a better solution. So the best option would be IrishChieftain's solution? Or is there a more elegant way to do it?
@AlexFrame As above, any reason why Button.PostBackUrl wouldn't work? What it will do is actually do a direct post to the url you set, resolving the PCI matter. I haven't tried the suggestion below, but a quick read of the code allows you to remove the server-side <form/> tag. Of course, that would also mean that some other code maybe affected (e.g. other controls on page that rely on default Postback behavior), which you will then need to adjust.
It "toggles" the form tag so no other controls are affected. I used it in five different E-Commerce sites I have developed. You can use JavaScript presuming it's enabled on all clients. Only other solution is to add a second pair of form tags and that's a hack that nobody has yet explained ;-)
1

Here is the solution I use:

http://jerschneid.blogspot.com/2007/03/hide-form-tag-but-leave-content.html

Caveat (own blog post):

http://codersbarn.com/post/2008/03/08/Solution-to-ASPNET-Form-PayPal-Problem.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.