Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP.NET Button1.PostBackUrl question...

Options
  • 10-08-2010 1:23pm
    #1
    Closed Accounts Posts: 585 ✭✭✭


    Hi Folks,

    I have an event handler set up in an aspx.cs page...

    protected void Button1_Click(object sender, System.EventArgs e)
    {

    //Run a few SQL insert statements and some other stuff

    //At the end of the code for the above actions, I have this line of code that redirects a user to the payment page for my internet online payment company gateway:

    Button1.PostBackUrl = "https://MyOnlinePaymentCompanyGatewayUrl.com/ThePaymentPage.cgi";


    }

    Basically my prob is that I have hidden form fields on the page that get sent to my 3rd party payment processing company when the button on the page is clicked. Using the above solution, at the moment, the user has to press the button twice for the script to work properly. On the first click, it seems that the SQL code is run and at the end of that, the Button1.PostBackUrl line is run, then you have to click it again for it to actually Postback to the required URL above.

    If I move the above line into the PageLoad or up to the top of the button event handler, then the page will redirect but the SQL code doesn't get run...

    If I use Respose.Redirect, the page will redirect but the 3rd party page but the form field seems to be embedded with the Button1 string info which is no use to me either...

    I'm sure there is an easy fix for this, I just can't see it, woods, trees, etc!

    Thanks in advance for any help...


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Sounds like you might have to execute the POST yourself from within the click event. Here's an example.

    So instead of Button1.PostBackUrl = "https://MyOnlinePaymentCompanyGatewayUrl.com/ThePaymentPage.cgi"; you'd call a method to do the HTTP POST to the payment processing company from with your event. At the moment all your doing is setting the PostBackURL property on your button, hence the need to click twice.
    protected void Button1_Click(object sender, System.EventArgs e)
    {
    
    //Run a few SQL insert statements and some other stuff
    
    //At the end of the code for the above actions, I have this line of code that redirects a user to the payment page for my internet online payment company gateway:
    uri = "https://MyOnlinePaymentCompanyGatewayUrl.com/ThePaymentPage.cgi";
    doPost(uri , params); // where params is an array of the parameters to pass to the payment website
    
    
    }
    
    private void doPost(String uri, String[] parameters) {
    // Executes the POST
    ...
    }
    


  • Closed Accounts Posts: 585 ✭✭✭MrDarcy


    Hi Phil, thanks for that, I was hoping that there would be something along the lines of:

    Button1.PostbackUrl.Trigger or something like that, so that instead of just declaring the PostBack event at the end of the Button event handler, it would actually execute the Postback after the other stuff was done...


  • Hosted Moderators Posts: 3,807 ✭✭✭castie


    Bit early for this but could you not attach the postback on page load?

    Unsure if the method would be called then the postback though.


  • Closed Accounts Posts: 585 ✭✭✭MrDarcy


    castie wrote: »
    Bit early for this but could you not attach the postback on page load?

    Unsure if the method would be called then the postback though.

    I was thinking maybe if I used the Button1 CommandName to handle everything, I dunno, am a bit bamboozled with this one! The Response.Redirect is almost exactly what I'm looking for, only the script on the referral page doen't like the fact that my form fields are embedded with the name of the Button...


  • Closed Accounts Posts: 585 ✭✭✭MrDarcy


    Just wondering is there any way I could stop the response.redirect script from changing the form field data to include what seems to be masterpage content or in any event, non form data???


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    If you set the PostBackUrl on page load then the first time the button is pressed all of the form data will go to the Url and the event handler will not fire.

    Just to explain how the PostBackUrl works:
    By default the button will be set to post back to the same url as the page that it is on. ASP.net uses it's own hidden form field (ViewState) to track what has happened across page loads and what the values the other forms fields should have etc. When you press the button, on the client side a value is written to the ViewState field to tell ASP.net that the button was pressed. The entire form is then submitted back to your .aspx page. When the page loads on the server side, one of the first things ASP.net does is to check the ViewState field to see what actions occured, in this case the button was pressed so it knows to execute the event handler you have setup for it.

    Setting the PostBackUrl will override this behaviour somewhat, the form will post to a different URL so your .aspx never loads on the server again and your code never fires. When you set it in your event handler, the postback has actually already happened so nothing gets directed anywhere else, until the button gets pressed again triggering another postback.

    The two obvious things you can do are what Evil Phil suggested and call a post from your server side code, this will also keep your visitor on your .aspx page unless you also want to use response.redirect to send them somewhere else; or you can do a Response.Redirect and include querystring parameters (if your 3rd party accepts them) i.e.
    Response.Redirect("http://www.3rdparty.com/payment.aspx?Value1=abc&Value2=xyz);
    


  • Closed Accounts Posts: 585 ✭✭✭MrDarcy


    stevenmu wrote: »
    If you set the PostBackUrl on page load then the first time the button is pressed all of the form data will go to the Url and the event handler will not fire.

    Just to explain how the PostBackUrl works:
    By default the button will be set to post back to the same url as the page that it is on. ASP.net uses it's own hidden form field (ViewState) to track what has happened across page loads and what the values the other forms fields should have etc. When you press the button, on the client side a value is written to the ViewState field to tell ASP.net that the button was pressed. The entire form is then submitted back to your .aspx page. When the page loads on the server side, one of the first things ASP.net does is to check the ViewState field to see what actions occured, in this case the button was pressed so it knows to execute the event handler you have setup for it.

    Setting the PostBackUrl will override this behaviour somewhat, the form will post to a different URL so your .aspx never loads on the server again and your code never fires. When you set it in your event handler, the postback has actually already happened so nothing gets directed anywhere else, until the button gets pressed again triggering another postback.

    The two obvious things you can do are what Evil Phil suggested and call a post from your server side code, this will also keep your visitor on your .aspx page unless you also want to use response.redirect to send them somewhere else; or you can do a Response.Redirect and include querystring parameters (if your 3rd party accepts them) i.e.
    Response.Redirect("http://www.3rdparty.com/payment.aspx?Value1=abc&Value2=xyz);
    

    Thanks so much for that Steve. The HTTP Post solution is a non runner for me because I need to land the user onto this 3rd party site with the form fields submitted because when they get to that page, they have to put in their credit card details and that can't happen on my site for security reasons because it's a redirect type payment solution using a payment company that is authorised by my bank.

    I've tried the response.redirect solution, it works except for the 3rd party form I'm landing users on, doesn't seem to like the data in one of the fields, (this was not a problem beforehand when I was using the PostBackURL solution), I had assumed because there was a masterpage content place holder ID being added into the value in one of the fields, I've since removed the masterpage from the solution and I'm still getting an error when using Response.Redirect... As you said, when I use the PostBackUrl solution I get the redirect working correctly but none of the stuff like SQL inserts in the button1 event hander gets done...

    Basically what is happening is that at the end of the online shopping experience, in the button event handler, all transactional data gets submitted into my mssql db. At the same time or more accurately on the same buttonclick, I need to redirect my customer to this payment page on another site where values like, MERCHANTID, AMOUNT, PAYREF get passed as well. Then the user puts in their credit card details and if the response is sucessful (the payment is good), then my db gets updated to reflect the fact that the transaction has been authorised or paid.


  • Closed Accounts Posts: 585 ✭✭✭MrDarcy


    Just in case any poor bugger comes across this thread and ended up pulling their hair out as I was until two minutes ago...!

    The solution for this is to set up a CommandName for the Button in the aspx page, use the PostBackUrl="http://www.YourPaymentGatewayURL.cgi" within the Button code on the aspx page,

    Then in the PageLoad in the codebehind, set up an if statement to deal with the CommandName, like:

    If(Button1.CommandName == "MyButtonWasJustClicked")

    {
    Do all my DB SQL Inserts, and any other stuff I need done...
    }

    It works for me anyway and as your man in Fr. Ted says, that's all that matters!


Advertisement