Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

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

Posted on 2007-11-20
10
Medium Priority
?
289 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
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 
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 2000 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

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

648 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