Solved

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

Posted on 2007-11-20
10
280 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
ID: 20324549
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
ID: 20324585
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
ID: 20331629
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Expert Comment

by:a1x
ID: 20331735
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
ID: 20331782
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
 
LVL 5

Expert Comment

by:a1x
ID: 20331806
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
ID: 20351340
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
ID: 20351578
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
ID: 20351623
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
ID: 20352198
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to count occurrences of each item in an array.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

809 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