Solved

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

Posted on 2007-11-20
10
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 42
Updating a single record in forEach using C#/Entity 7 26
How to build a web site 17 57
orderby list (from Json) 1 30
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
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…

726 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