Solved

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

Posted on 2007-11-20
10
278 Views
Last Modified: 2013-11-26
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.
0
Comment
Question by:jackiemeck
  • 5
  • 5
10 Comments
 
LVL 5

Expert Comment

by:a1x
Comment Utility
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)
0
 
LVL 5

Expert Comment

by:a1x
Comment Utility
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
0
 

Author Comment

by:jackiemeck
Comment Utility
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,
0
 
LVL 5

Expert Comment

by:a1x
Comment Utility
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.
0
 

Author Comment

by:jackiemeck
Comment Utility
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Expert Comment

by:a1x
Comment Utility
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();
0
 

Author Comment

by:jackiemeck
Comment Utility
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?
0
 
LVL 5

Accepted Solution

by:
a1x earned 500 total points
Comment Utility
It will work if you can make your web page source look like:

<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function postToAnotherPage(URL){
            document.form1.action = URL;
            document.form1.__VIEWSTATE.name = "NOVIEWSTATE";
            document.form1.submit();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="hidden" name="imHidden" value="myValue" />
        <input id="Button1" type="button" value="Post to Another Page" onclick="postToAnotherPage('Default.aspx');" />
    </div>
    </form>
</body>
</html>
0
 

Author Comment

by:jackiemeck
Comment Utility
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?
0
 

Author Comment

by:jackiemeck
Comment Utility
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Read about why website design really matters in today's demanding market.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now