Link to home
Start Free TrialLog in
Avatar of jackiemeck
jackiemeck

asked on

How to 'Post' from ASP.net to an external site using .asp

I have a shopping cart page in ASP.net / VB.net that uses a datagrid.  The datagrid is filled with a list of products containing item, quantity and price.

I have a legacy application that needs to receive a "Submit" from the .aspx page.  It uses "request.form" to extract multiple variables.

In addition to receiving the ones from the datagrid, I need to receive some hidden values.  

There are three parts:
1) How do you get a Submit button to call an external web site.
2) How do you add HTML dynamically to define the hidden values
3) How do you post the data from the datagrid (I may be able to use #2 to solve this).

Thanks in advance.
Avatar of a1x
a1x

1) To submit to another page, change the form.action through javascript.  If the other page isn't ASP.NET, you can remove the viewstate line. RegisterStartupScript puts the script at the bottom of the page.

            Dim sJavaScript As String = "<"
                "<script language='javascript'>" & vbCrLf & _
                "document.Form1.action = 'http://localhost/myOthepage.aspx;" & vbCrLf & _
                "document.Form1.__VIEWSTATE.name = 'NOVIEWSTATE';" & vbCrLf
                "</script>"
            RegisterStartupScript("Startup", sJavaScript)
1) To submit to another page, change the form.action through javascript.  If the other page isn't ASP.NET, you can remove the viewstate line. RegisterStartupScript puts the script at the bottom of the page.
            Dim sJavaScript As String = "<script language='javascript'>" & vbCrLf & _
                "document.Form1.action = 'http://localhost/etwebexpense/etwExpense.aspx';" & vbCrLf & _
                "document.Form1.__VIEWSTATE.name = 'NOVIEWSTATE';" & vbCrLf & _
                "</script>"
            RegisterStartupScript("Startup", sJavaScript)

2) Add hidden HTML dynamically.  You can use a placeholder and the following code, or just add it to #1.
  PlaceHolder1.Controls.Add(New LiteralControl("<INPUT type='hidden' name='imHidden' value='myValue' >"))

3) Pull the data out of the grid and use #1 or #2
Avatar of jackiemeck

ASKER

I think we're on to something here...I was able to get #1 to work, but #2 returns an error in line 71. I added line #70.  (Note: This code is in the same subroutine as #1 above.

ERROR: Object reference not set to an instance of an object.
Line 70:         Dim placeholder1 As PlaceHolder
Line 71:         PlaceHolder1.Controls.Add(New LiteralControl("<INPUT type='hidden' name='imHidden' value='myValue' >"))

What am I missing?

Thanks,
The code

PlaceHolder1.Controls.Add(New LiteralControl("<INPUT type='hidden' name='imHidden' value='myValue' >"))

assumes that you have created a placeholder control in the aspx file.
Roger on the Placeholder...that fixed it.

Is Javascript the only way to get ASP.net to go to another page?  Seems like there should be an internal function for that.

This feature redirects all of the buttons on the page.  I need "Begin Checkout" to go to the .ASP site as your code allows, but also need a button called "Empty Cart" to stay in the same .ASPX page.  

Thoughts on how to do that?

Thanks for your help.
There may be.  It is integral to the ASP.NET model to post back to the same page, tho.

To make only one button submit to the other page, call the the above code from the button's click event, and add the following command to actually submit the form:

document.Form1.submit();
I tried your suggestion, but it doesn't seem to add the hidden script/form values in the button press.

I build a public string when the page first loads so it will be available on the button press.  This string includes the <script> values and the <input> values.  I included 'document.form1.submit()' inside the script block. Then, I use this command:        
     RegisterStartupScript("Startup", mycart)
in the button event.

The page simply reloads itself and the source does not show the "mycart" values.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of a1x
a1x

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That makes great sense...I just haven't figured out how to add the "onclick" to the .Net button.  Do I have to create this button manually or some unique way?
Scratch the last question...I was able to add "onclick" to the button using the following command.
     Button2.Attributes.Add("onclick", "postToAnotherPage('default.asp')")

I also had to remove the following line from the script to be able to call the .ASP page I need.
    document.form1.__VIEWSTATE.name = "NOVIEWSTATE";

Thanks for your expert help.