Google Answers Logo
View Question
 
Q: Create dynamic data and post to a server. ( Answered 4 out of 5 stars,   2 Comments )
Question  
Subject: Create dynamic data and post to a server.
Category: Computers > Programming
Asked by: tonymast-ga
List Price: $25.00
Posted: 10 Mar 2003 19:52 PST
Expires: 09 Apr 2003 20:52 PDT
Question ID: 174465
I'm using VB .NET 
I need to have the customer click a button and raise an event. 
I then need to process some information and prepare values to send. 
I then need to create the example below (or equivalent) and send it in
the VB .NET
event. 
 
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Button1.Click 
        ‘I can get here
        'code goes here prepare dynamic values. (I can do this part)

Taxes = Cost * 8.75
Price = Shipping + Cost

‘ then create the code below and post it?  (this next part is what I
‘need

PostIt = “<FORM ACTION="https://www.whatever.com/cgi-bin/webscr"
_METHOD="post" TARGET="XYZ" ID="Form2"> “

PostIt += “  <INPUT TYPE="hidden" VALUE= Price NAME=amount>   “
PostIt += “ <INPUT TYPE="hidden" VALUE= Taxes NAME=TaxAmount>  “
PostIt += “ <INPUT TYPE="hidden" VALUE= Shipping NAME=Ship>   “
PostIt += “  </form>  “

PostIt   ‘  Now somehow post it to the server



End Sub 

Below is the cut and paste code I got from the server that the data
will be posted too. (I edited it with the databinder.eval)the code
below works correct.
 
<FORM ACTION="https://www.whatever.com/cgi-bin/webscr" METHOD="post" 
TARGET="XYZ" ID="Form2"> 
<INPUT TYPE="hidden" VALUE="_cart" NAME="cmd">  
<INPUT TYPE="hidden" VALUE="me@me.com" NAME="business" 
<INPUT TYPE="hidden" VALUE='<%# DataBinder.Eval(ctype(Container, 
DataListItem).DataItem, "Name") %>>' NAME=item_name> 
<INPUT TYPE="hidden" VALUE='<%# DataBinder.Eval(CType(Container, 
DataListItem).DataItem, "ItemID") %>' NAME=item_number> 
<INPUT TYPE="hidden" VALUE='<%# DataBinder.Eval(CType(Container, 
DataListItem).DataItem, "RetailPrice") %>' NAME=amount> 
<INPUT TYPE="hidden" VALUE="1" NAME="no_note"> 
<INPUT TYPE="hidden" VALUE="USD" NAME="currency_code"> 
P align="right"> 
<INPUT TYPE="image" HEIGHT="23" ALT="xxx" WIDTH="87" 
SRC="https://www.me.com/images/x-click-but22.gif" BORDER="0" 
NAME="I1"> 
<INPUT TYPE="hidden" VALUE="1" NAME="add"></P> 
/FORM>
Answer  
Subject: Re: Create dynamic data and post to a server.
Answered By: mmastrac-ga on 22 Mar 2003 13:21 PST
Rated:4 out of 5 stars
 
Easy!  Use the WebRequest class in .NET to prepare a POST request:

' Create our new form-encoded post
' This needs to look like "cmd=_cart&business=me@me.com&x=y&..."
Dim PostString as New String
PostString += "cmd=" + UrlEncode( "_cart" )
PostString += "&"
PostString += "business=" + UrlEncode( "me@me.com" )
PostString += "&"
PostString += "x=" + UrlEncode( "something else" )
etc...

' Create a new WebRequest
Dim req as HttpWebRequest = WebRequest.Create(
"https://www.whatever.com/cgi-bin/webscr" )
' Make it a post
req.Method = "POST"
req.AddHeader( "Content-Type", "application/x-www-form-encoded" )
req.ContentLength = PostString.Length
' Send the post data
Dim sw as StreamWriter = new StreamWriter( req.GetRequestStream() )
sw.WriteLine( PostString )
' Read the response
Dim sr as StreamReader = new StreamReader( req.GetResponseStream() )
Dim ResponseText = sr.ReadToEnd()

The response from the server will be in ResponseText for you to use if
you need it.

Let me know if you need more.

Clarification of Answer by mmastrac-ga on 22 Mar 2003 13:35 PST
Let me just add that for each of the HIDDEN form fields, you need to
add an item to the PostString.

For the image, you need to add two fields to the PostString specifying
the coordinates clicked on.  Since you're not clicking on the image,
just use 1 for both.

This should be the *full* skeleton you'll need to use.  I've used
three dots to indicate the bits where your calculated values should be
inserted:

PostString += "cmd=" + UrlEncode( "_cart" ) 
PostString += "&" 
PostString += "business=" + UrlEncode( "me@me.com" ) 
PostString += "&" 
PostString += "item_name=" + UrlEncode( ... ) 
PostString += "&" 
PostString += "item_number=" + UrlEncode( ... ) 
PostString += "&"  
PostString += "amount=" + UrlEncode( ... ) 
PostString += "&" 
PostString += "no_note=" + UrlEncode( "1" ) 
PostString += "&" 
PostString += "currency_code=" + UrlEncode( "USD" ) 
PostString += "&" 
PostString += "I1.x=" + UrlEncode( "1" ) 
PostString += "&" 
PostString += "I1.y=" + UrlEncode( "1" ) 
PostString += "&"
PostString += "add=" + UrlEncode( "1" )

Your final string should look something like this:
cmd=_cart&business=me%40me.com&item_name=somename&item_number=somenumber&amount=someamount&no_note=1&currency_code=USD&I1.x=1&I1.y=1&add=1

Request for Answer Clarification by tonymast-ga on 22 Mar 2003 14:51 PST
PostString += "cmd=" + UrlEncode( "_cart" )  
PostString += "&"  
PostString += "business=" + UrlEncode( "me@me.com" )  
PostString += "&"  
PostString += "item_name=" + UrlEncode( ... )  
PostString += "&"  
PostString += "item_number=" + UrlEncode( ... )  
PostString += "&"   
PostString += "amount=" + UrlEncode( ... )  
PostString += "&"  
PostString += "no_note=" + UrlEncode( "1" )  
PostString += "&"  
PostString += "currency_code=" + UrlEncode( "USD" )  
PostString += "&"  
PostString += "I1.x=" + UrlEncode( "1" )  
PostString += "&"  
PostString += "I1.y=" + UrlEncode( "1" )  
PostString += "&" 
PostString += "add=" + UrlEncode( "1" ) 


' Create a new WebRequest 
Dim req as HttpWebRequest =
WebRequest.Create("https://www.whatever.com/cgi-" )
' Make it a post 
req.Method = "POST" 
req.AddHeader( "Content-Type", "application/x-www-form-encoded" ) 
req.ContentLength = PostString.Length 
' Send the post data 
Dim sw as StreamWriter = new StreamWriter( req.GetRequestStream() ) 
sw.WriteLine( PostString ) 
' Read the response 
Dim sr as StreamReader = new StreamReader( req.GetResponseStream() ) 
Dim ResponseText = sr.ReadToEnd() 


This looks just what i was looking for.
I will try this monday 3/24/03.

I lost my wallet one week ago and had to cancel all my credit cards
and the card used in google answers was one that had to be canceled. I
am expecting to get new cards next week (3/24 and on) please be
assured I will correct this so you receive payment as soon as
possible.

Thank You
Tonymast@msn.com

Request for Answer Clarification by tonymast-ga on 23 Mar 2003 18:05 PST
I've change credit card info and all is well with that now, sorry for
any inconvience it caused you.

I've added the following to my page.

Imports System.Web
Imports System.IO

however I cannot find any refferences to HTTPWEBREQUEST

I'm getting the following errors

 "name is not declared"  for URLENCODE

 and

"name not defined" for  HTTPWEBREQUEST.


I was getting an error on 

Dim PostString as New String  
"overload resolution failed because no accessible 'NEW' excepts this
number of arguments"
I changed it to Dim PostString as string with no no error. is this ok?

can you please clarify this for me.

Thanks
Tony

Request for Answer Clarification by tonymast-ga on 23 Mar 2003 20:41 PST
What I've done is
Added 
(httpUtility) to UrlEncode - HttpUtility.UrlEncode this dosn't create
an error any longer is this correct way?

I added
Imports System.Net
I no longer get errors on 
Dim req as HttpWebRequest = WebRequest.Create(
"https://www.whatever.com/cgi-bin/webscr" )

however
------------------------------------------------------------------------
I now get errors on 
req.AddHeader( "Content-Type", "application/x-www-form-encoded" ) 
"addheader is not a member of system.net.httpwebrequest
needs a solution.
------------------------------------------------------------------------
I was getting an error  on
Dim sr as StreamReader = new StreamReader( req.GetResponseStream() ) 
"getresponsestream is not a member of system.net.httpwebrequest"

so I added Dim req1 As HttpWebResponse and changed to
req1.GetResponseStream() ) this dosn't produce an error. Is This
correct?

Thanks
Tony Mastracchio

Clarification of Answer by mmastrac-ga on 23 Mar 2003 22:06 PST
My apologies - I had assumed that certain Import lines would be
present.  I also managed to make a few typos in my code:

-- first --

req.AddHeader( 

should be:

req.Headers.Add(

-- and --

Dim sr as StreamReader = new StreamReader( req.GetResponseStream() )

should be:

Dim sr as StreamReader = new StreamReader(
req.GetResponse().GetResponseStream() )

You are correct with respect to the "PostString" line.  Use the line I
have written above to fix the problem with "GetResponseStream() is not
a member of HttpWebRequest".  The reason is that you need to get the
response object from the request before asking for the content of the
response.

This code should work for you, but let me know ASAP if there are any
issues with it.  I'm sorry the code I gave you didn't compile right
out-of-the-box.

Sincerely,
Matt Mastracci.

Request for Answer Clarification by tonymast-ga on 24 Mar 2003 10:45 PST
For some reason (or google bug) I didn't have a clarify answer button.
So I created another question for $2. Question ID: 180327  so if you
can go there and make believe you answered that question.
So I go baqck into my question and now there is a clarify button so
here it is.



I'm getting the following errors on 
 req.Headers.Add("Content-Type", "application/x-www-form-encoded") 
"This header must be modified with the appropriate property" 
 
Thanks 
Tony

Clarification of Answer by mmastrac-ga on 24 Mar 2003 12:25 PST
Now I have the clarify answer link (weird).  I could only post a
comment the last time.

Here is the final, corrected, up-to-date code (note the ContentType
change in this one):

---
 
--- Form1.vb --- 
Imports System.Web 
Imports System.Net 
Imports System.IO 
 
Public Class Form1 
    Inherits System.Windows.Forms.Form 
 
#Region " Windows Form Designer generated code " 
 
    Public Sub New() 
        MyBase.New() 
 
        'This call is required by the Windows Form Designer. 
        InitializeComponent() 
 
        'Add any initialization after the InitializeComponent() call 
 
    End Sub 
 
    'Form overrides dispose to clean up the component list. 
    Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
        If disposing Then 
            If Not (components Is Nothing) Then 
                components.Dispose() 
            End If 
        End If 
        MyBase.Dispose(disposing) 
    End Sub 
 
    'Required by the Windows Form Designer 
    Private components As System.ComponentModel.IContainer 
 
    'NOTE: The following procedure is required by the Windows Form
Designer
    'It can be modified using the Windows Form Designer.   
    'Do not modify it using the code editor. 
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        ' 
        'Form1 
        ' 
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) 
        Me.ClientSize = New System.Drawing.Size(292, 273) 
        Me.Name = "Form1" 
        Me.Text = "Form1" 
 
    End Sub 
 
#End Region 
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim PostString As String 
        PostString += "cmd=" + HttpUtility.UrlEncode("_cart") 
        PostString += "&" 
        PostString += "business=" + HttpUtility.UrlEncode("me@me.com")
        PostString += "&" 
        PostString += "item_name=" + HttpUtility.UrlEncode("") 
        PostString += "&" 
        PostString += "item_number=" + HttpUtility.UrlEncode("") 
        PostString += "&" 
        PostString += "amount=" + HttpUtility.UrlEncode("") 
        PostString += "&" 
        PostString += "no_note=" + HttpUtility.UrlEncode("1") 
        PostString += "&" 
        PostString += "currency_code=" + HttpUtility.UrlEncode("USD")
        PostString += "&" 
        PostString += "I1.x=" + HttpUtility.UrlEncode("1") 
        PostString += "&" 
        PostString += "I1.y=" + HttpUtility.UrlEncode("1") 
        PostString += "&" 
        PostString += "add=" + HttpUtility.UrlEncode("1") 
 
        ' Create a new WebRequest   
        Dim req As HttpWebRequest =
WebRequest.Create("http://www.grack.com/cgi-bin/test")
        ' Make it a post   
        req.Method = "POST" 
        req.ContentType = "application/x-www-form-urlencoded" 
        req.ContentLength = PostString.Length 
        ' Send the post data   
        Dim sw As StreamWriter = New
StreamWriter(req.GetRequestStream())
        sw.Write(PostString) 
        sw.Close() 
        ' Read the response   
        Dim sr As StreamReader = New
StreamReader(req.GetResponse().GetResponseStream())
        Dim ResponseText = sr.ReadToEnd() 
        MsgBox(ResponseText) 
    End Sub 
End Class

Request for Answer Clarification by tonymast-ga on 24 Mar 2003 14:06 PST
OK, one more thing.
I got it to work However. 
It is supposed to open another instance of the browser and display a
shopping cart, it dosn't do that.

I'm wondering if it has to do with the original question.

<FORM ACTION="https://www.whatever.com/cgi-bin/webscr"
_METHOD="post" TARGET="XYZ" ID="Form2"> 

in your solution do you take into account the TARGET="XYZ" 

Thanks
Tony

Clarification of Answer by mmastrac-ga on 24 Mar 2003 16:46 PST
Okay - here is more code that opens a browser window with the response
and allows the user to interact with the page as if it came from the
server.

--- Form1.vb ---

Imports System.Web
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form
Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim PostString As String
        Dim PostURL As String = "http://www.grack.com/cgi-bin/test"
        PostString += "cmd=" + HttpUtility.UrlEncode("_cart")
        PostString += "&"
        PostString += "business=" + HttpUtility.UrlEncode("me@me.com")
        PostString += "&"
        PostString += "item_name=" + HttpUtility.UrlEncode("")
        PostString += "&"
        PostString += "item_number=" + HttpUtility.UrlEncode("")
        PostString += "&"
        PostString += "amount=" + HttpUtility.UrlEncode("")
        PostString += "&"
        PostString += "no_note=" + HttpUtility.UrlEncode("1")
        PostString += "&"
        PostString += "currency_code=" + HttpUtility.UrlEncode("USD")
        PostString += "&"
        PostString += "I1.x=" + HttpUtility.UrlEncode("1")
        PostString += "&"
        PostString += "I1.y=" + HttpUtility.UrlEncode("1")
        PostString += "&"
        PostString += "add=" + HttpUtility.UrlEncode("1")

        ' Create a new WebRequest  
        Dim req As HttpWebRequest = WebRequest.Create(PostURL)
        ' Make it a post  
        req.Method = "POST"
        req.ContentType = "application/x-www-form-encoded"
        req.ContentLength = PostString.Length
        ' Send the post data  
        Dim sw As StreamWriter = New
StreamWriter(req.GetRequestStream())
        sw.Write(PostString)
        sw.Close()
        ' Read the response  
        Dim sr As StreamReader = New
StreamReader(req.GetResponse().GetResponseStream())
        Dim ResponseText = sr.ReadToEnd()

        ' Write out the data to shoppingcart.html
        Dim swOutput As New StreamWriter("shoppingcart.html")
        swOutput.WriteLine(Regex.Replace(ResponseText, "<title>",
"<base href=""" + PostURL + """><title>"))
        swOutput.Close()

        ' Start up the browser to see the response
        Process.Start("shoppingcart.html")

        MsgBox(ResponseText)
    End Sub
End Class

Request for Answer Clarification by tonymast-ga on 24 Mar 2003 19:47 PST
Ok maybe this wasn't so easy, I know you spent alot of time on this so
I will tip you.
Once the original (my original question)HTML was sent to the remote
server it produced another browser displaying a shopping cart, your
example did not work (explained below). something in the original HTML
triggers the remote server to create the cart and display it.

errors
--------------------------------------------
I do not have the authority to create a file "shoppingcart.html". I
tried to qualify it "http://www.mywebsite.com/shoppingcart.com". This
is not supported
"URI formats are not supported"
--------------------------------------------
Process.start("shoppingcart")
produces a syntex error
"name "process" not decleared.
--------------------------------------------
By the way i'm using web forms not windows forms, if that helps any.

Thanks
Tony

Request for Answer Clarification by tonymast-ga on 24 Mar 2003 23:29 PST
I believe the problem is with the TARGET protion of the <FORM> tag in
the HTML.
I'm still getting errors as described in the previous Request for
clarification. Even if corrected this solution will only display the
current return information. I need for the cart display to be
performed by the remote server. So it can manage multiple orders and
other cart functions. Which it does if I execute the HTML seen in my
original question.

Thanks
Tony

Clarification of Answer by mmastrac-ga on 25 Mar 2003 06:59 PST
I didn't realize you were building a Web application.  That changes
things a bit, but not too badly.  Sorry about the misunderstanding...

You'll need to create a second WebForm page, call it something like
"doPost.aspx".  Edit the aspx page to look like this:

<body>
<FORM ACTION="https://www.whatever.com/cgi-bin/webscr" METHOD="post"
TARGET="XYZ" ID="Form">  
<INPUT TYPE="hidden" VALUE="_cart" NAME="cmd">   
<INPUT TYPE="hidden" VALUE="me@me.com" NAME="business">
<INPUT TYPE="hidden" VALUE='<%= HttpServerUtility.UrlEncode(
Request.QueryString( "item_name" ) %>' NAME=item_name>
<INPUT TYPE="hidden" VALUE='<%= HttpServerUtility.UrlEncode(
Request.QueryString( "item_number" ) %>' NAME=item_number>
<INPUT TYPE="hidden" VALUE='<%= HttpServerUtility.UrlEncode(
Request.QueryString( "amount" ) %>' NAME=amount>
<INPUT TYPE="hidden" VALUE="1" NAME="no_note">  
<INPUT TYPE="hidden" VALUE="USD" NAME="currency_code">  
<INPUT TYPE="hidden" VALUE="1" NAME="I1.x">  
<INPUT TYPE="hidden" VALUE="1" NAME="I1.y">  
<INPUT TYPE="hidden" VALUE="1" NAME="add"></P>  
</FORM>
<script defer>document.getElementById('Form').submit();</script>
</body>

In the render method of your other page, put the following:

output.Write( "<iframe style=""display:none;""
src=""doPost.aspx?item_name="& HttpServerUtility.UrlEncode( item_name
) & "&item_number="& HttpServerUtility.UrlEncode( item_number )
&"&amount="& HttpServerUtility.UrlEncode( amount ) &"></iframe>" )

What this method does is creates a hidden frame pointing at the other
page you've created.  It passes parameters to this hidden frame
telling it what to send to the shopping cart.  The hidden frame then
renders the appropriate form and posts it immediately (no click
required).  It should pop up in a new window at the site you're
pointed at.

Request for Answer Clarification by tonymast-ga on 25 Mar 2003 11:49 PST
is this Visual Basic .Net code?

output.Write( "<iframe style=""display:none;""
src=""doPost.aspx?item_name="& HttpServerUtility.UrlEncode( item_name
) & "&item_number="& HttpServerUtility.UrlEncode( item_number )
&"&amount="& HttpServerUtility.UrlEncode( amount ) &"></iframe>" )

heres what I have

 Private Sub dtalstSearchResults_Itemcommand(ByVal source As
System.Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
dtalstSearchResults.ItemCommand
       If (e.CommandName = "AddToCart") Then
              Dim amount As Double 
              Dim Item_Number as int32    
              Amount = 12.75 + 2.50              
              output.Write( "<iframe style=""display:none;""
              src=""doPost.aspx?item_name="&
HttpServerUtility.UrlEncode(             item_name) & "&item_number="&
HttpServerUtility.UrlEncode(             item_number )
              &"&amount="& HttpServerUtility.UrlEncode( amount )      
         &"></iframe>" )

             end if
end sub

error i'm getting is 
"name "output" not declared"

if you go to tesscollections.com and shopp a little. add to shopping
cart a couple of things you can see how it works.

thanks
Tony

Clarification of Answer by mmastrac-ga on 25 Mar 2003 12:38 PST
Thanks for the demo link - I have a better idea of what you are
looking for now.

I've got a better idea of what you should do to get this working now. 
Here are some steps to follow to finish this off:

Add this item in your form's .aspx page:

<div id="PostData" runat="server"></div>

Now, add this line to your click method:

        PostData.InnerHtml = "<iframe style=""display:none;""
src=""doPost.aspx?item_name=" & Server.UrlEncode("item_name") &
"&item_number=" & Server.UrlEncode("item_number") & "&amount=" &
Server.UrlEncode("amount") & """></iframe>"

(replacing the item_number, amount and item_name with your calculated
values).

I tried this out and it looks like it works to me.  This is by far the
easiest solution that I've come across.  Please try this out and let
me know how it works.

Here is the test page I used:

WebForm1.aspx.vb:
-----------------
Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents PostData As
System.Web.UI.HtmlControls.HtmlGenericControl
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form
Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
        PostData.InnerHtml = "<iframe src=""doPost.aspx?item_name=" &
Server.UrlEncode("item_name") & "&item_number=" &
Server.UrlEncode("item_number") & "&amount=" &
Server.UrlEncode("amount") & """></iframe>"
    End Sub
End Class

WebForm1.aspx:
--------------

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb"
Inherits="TestWebApplication1.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
		<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<div id="PostData" runat="server">
				<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 245px;
POSITION: absolute; TOP: 121px" runat="server"
Text="Button"></asp:Button></div>
		</form>
	</body>
</HTML>

doPost.aspx:
------------

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="doPost.aspx.vb" Inherits="TestWebApplication1.doPost"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<title>doPost</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
		<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
	</head>
	<body> 
<FORM ACTION="http://www.grack.com/cgi-bin/test" METHOD="post"
TARGET="XYZ" ID="Form">   
<INPUT TYPE="hidden" VALUE="_cart" NAME="cmd">    
<INPUT TYPE="hidden" VALUE="me@me.com" NAME="business"> 
<INPUT TYPE="hidden" VALUE='<%=
Server.UrlEncode(Request.QueryString.Item( "item_name" )) %>'
NAME=item_name>
<INPUT TYPE="hidden" VALUE='<%=
Server.UrlEncode(Request.QueryString.Item( "item_number" )) %>'
NAME=item_number>
<INPUT TYPE="hidden" VALUE='<%=
Server.UrlEncode(Request.QueryString.Item( "amount" )) %>'
NAME=amount>
<INPUT TYPE="hidden" VALUE="1" NAME="no_note">   
<INPUT TYPE="hidden" VALUE="USD" NAME="currency_code">   
<INPUT TYPE="hidden" VALUE="1" NAME="I1.x">   
<INPUT TYPE="hidden" VALUE="1" NAME="I1.y">   
<INPUT TYPE="hidden" VALUE="1" NAME="add"></P>   
</FORM> 
<script defer>document.getElementById('Form').submit();</script> 
</body> 
</html>

Request for Answer Clarification by tonymast-ga on 27 Mar 2003 10:36 PST
I've given up

I can't get any of your solution(s) to work.

Clarification of Answer by mmastrac-ga on 27 Mar 2003 12:24 PST
I tried the last piece of code I posted locally and everything worked
as I expected.  If you are still looking for an answer, I'm willing to
keep helping you until you have something that works for you.

Please let me know which pieces of code are not working and I can help
you through it.

I can also post the sample files on an external webserver to make it
easier for you to download.

Let me know what I can do to help.

Request for Answer Clarification by tonymast-ga on 27 Mar 2003 14:43 PST
OK, if you are willing to stick with it and me that is encouraging.
Below is the exact (hard coded) HTML for an item on my web site. This
works fine.  This code is executed and brings up a Paypal shopping
cart (in another browser). The cart has a continue shopping button
which returns focus to the page that sent it in the original browser.
Customer purchases another product and Paypal shopping cart adds it to
the cart. and so on ...

I want to execute the code below in a click event in Visual Basic .Net
codebehind. So I can Dynamiclly create all of the hard coded values.
Then post it

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr"
method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business"
value="sales@tesscollections.com">
<input type="hidden" name="item_name" value="Garnet & Peridot
Earrings">
<input type="hidden" name="item_number" value="E_GAPE001">
<input type="hidden" name="amount" value="9.99">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<p align="right">
<input type="image" src="https://www.paypal.com/images/x-click-but22.gif"
border="0" name="I1" alt="Make payments with PayPal - it's fast, free
and secure!" width="87" height="23">
<input type="hidden" name="add" value="1">
</p>
</form>
-------------------------------------------
I tried the code below along with the other code you gave me the last
time. I know this is executing because I put the label1.text = amount
& " " & Item_name and those values are displayed in label1.

 Private Sub dtalstSearchResults_Itemcommand(ByVal source As
System.Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
dtalstSearchResults.ItemCommand
             If (e.CommandName = "AddToCart") Then
            Dim amount As Double
            Dim item_name As String
            Dim Item_number As String
            amount = "13.95"
            Item_number = "2468"
            item_name = "Rings"
            PostData.InnerHtml = "<iframe
src=""Postit.aspx?item_name=" & Server.UrlEncode("item_name") &
"&item_number=" & Server.UrlEncode("item_number") & "&amount=" &
Server.UrlEncode("amount") & """></iframe>"

label1.text = amount & " " & Item_name
end sub


----------------------------------------------------------------
(I called your dopost.aspx postit.aspx)
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="postit.aspx.vb" Inherits="TessWeb.postit"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<TITLE>postit</TITLE>
		<META name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
		<META name="CODE_LANGUAGE" content="Visual Basic 7.0">
		<META name="vs_defaultClientScript" content="JavaScript">
		<META name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
<BODY> 
<FORM target="paypal" action="https://www.paypal.com/cgi-bin/webscr"
method="post">
<INPUT TYPE="hidden" VALUE="_cart" NAME="cmd">     
<INPUT TYPE="hidden" VALUE="sales@tesscollections.com"
NAME="business">
<INPUT TYPE="hidden"
VALUE='<%=Server.UrlEncode(Request.QueryString.Item( "item_name" ))
%>' NAME=item_name>
<INPUT TYPE="hidden"
VALUE='<%=Server.UrlEncode(Request.QueryString.Item( "item_number" ))
%>' NAME=item_number>
<INPUT TYPE="hidden"
VALUE='<%=Server.UrlEncode(Request.QueryString.Item( "amount" )) %>'
NAME=amount>
<INPUT TYPE="hidden" VALUE="1" NAME="no_note">    
<INPUT TYPE="hidden" VALUE="USD" NAME="currency_code">    
<INPUT TYPE="hidden" VALUE="1" NAME="I1.x">    
<INPUT TYPE="hidden" VALUE="1" NAME="I1.y">    
<INPUT TYPE="hidden" VALUE="1" NAME="add"></P>    
</FORM>  
<SCRIPT defer>document.getElementById('Form').submit();</SCRIPT>  
</BODY>  


</HTML>
---------------------------------------------------------------------


When I run this.
On my products page is a button on top of page called Button  that I
do not want. It is out of place on the page. I have my click button
next to each product.
When I rasie the click event,
The product page gets executed again (like a refresh) with a large (6"
by 10") empty frame? No other broswer and no paypal cart.

Thanks
Tony

Request for Answer Clarification by tonymast-ga on 27 Mar 2003 14:54 PST
I removed the button I embedded my linkbutton in the <div> you gave
me. Know I'm getting
"Object reference not set to an instance of an object."
on
PostData.InnerHtml = "<iframe src=""Postit.aspx?item_name=" &
Server.UrlEncode("item_name") & "&item_number=" &
Server.UrlEncode("item_number") & "&amount=" &
Server.UrlEncode("amount") & """></iframe>"

Thanks
Tony

Clarification of Answer by mmastrac-ga on 27 Mar 2003 16:10 PST
The button I had on the page was for testing the setup.  Removing it
should not cause any problems.

Your symptom of "Object reference not set..." sounds like the <div>
with the ID of "PostData" is missing the 'runat="server"' attribute on
the aspx page or is missing completely.  Ensure that somewhere on the
.aspx page, there is one (and only one) tag that looks like this:

<div id="PostData" runat="server" style="display:none;"></div>

Put it right at the bottom of your page, before the </body> tag.  

Can you post the aspx page you are working with?

Request for Answer Clarification by tonymast-ga on 28 Mar 2003 12:39 PST
When I raise the click event, 
The product page gets executed again (like a refresh) with a large (6"
by 10") empty frame? No other broswer and no paypal cart.

try 
http://www.tesscollections.com/tessweb/TessSearchResults.aspx?Cat=Ring&Words=stunning
 
Thanks 
Tony

Request for Answer Clarification by tonymast-ga on 28 Mar 2003 12:44 PST
When I raise the click event,  
The product page gets executed again (like a refresh) with a large (6"
by 10") empty frame? No other broswer and no paypal cart. 
 
try  
http://www.tesscollections.com/tessweb/TessSearchRequest.aspx
click search button
click add to cart
  
Thanks  
Tony

Request for Answer Clarification by tonymast-ga on 28 Mar 2003 14:39 PST
Sorry for so many clarifications.
I've been working on this I found an error/typo and here are 2
problems.
first let me say I'm adding products and pulling up paypals shopping.
That part now works so we are close.

1) When I raise the click event,   
The product page gets executed again (like a refresh) with a large (6"
by 10") empty frame? 

2) When the paypal cart comes up the quanity is 2? So I'm trying to
figure out if maybe for some reason the postit.aspx is executing 2
times or is there something wrong with the way we're sending the data.

thanks
Tony

Request for Answer Clarification by tonymast-ga on 28 Mar 2003 15:05 PST
the only issue I now have is :)

When the paypal cart comes up the quanity is 2? So I'm trying to
figure out if maybe for some reason the postit.aspx is executing 2
times or is there something wrong with the way we're sending the data.


 
thanks 
Tony

Clarification of Answer by mmastrac-ga on 28 Mar 2003 16:04 PST
I just noticed that you have SmartNavigation enabled.  This often
causes problems with more complicated page layouts like your own.  In
your .aspx page, please look for the following:

<%@ Page ... smartNavigation="True" ... %>

and change the smartNavigation bit to:

<%@ Page ... smartNavigation="False" ... %>

Clarification of Answer by mmastrac-ga on 28 Mar 2003 16:07 PST
Just to clarify my last post - it's likely that because of this
smartNavigation, your form is getting posted to PayPal twice.  I would
recommend against using this ASP.NET feature - it tends to cause
unexpected behaviour in some cases (like the behaviour that you are
seeing).

Clarification of Answer by mmastrac-ga on 28 Mar 2003 16:35 PST
I'm going to guess here that you enabled smartnav to get around the
form scrolling to the top each time you've added an item.  I'll
pre-emtively answer this question for you.  :)  Here are the steps:

1.  Create an IFRAME tag on your .aspx page like so:

<iframe id="PostDataFrame" name="PostDataFrame"
style="display:none;"></iframe>

2.  When you create each of your "Add To Cart" linkbuttons, set the
NavigateUrl like so:

mylinkbtn.NavigateUrl = "doPost.aspx?item_name="&
HttpServerUtility.UrlEncode( .. your calculated value .. ) &
"&item_number="&
HttpServerUtility.UrlEncode( .. your calculated value .. )&"&amount="&
HttpServerUtility.UrlEncode( .. your calculated value .. )

and set the Target to PostDataFrame like so:

mylinkbtn.Target = "PostDataFrame"

3.  Now, when you click the links, it won't post back, but it will
load the post page into the hidden form, popping up your PayPal page
when the hidden page does its post.

Let me know if this is what you are looking for!

Matt.

Request for Answer Clarification by tonymast-ga on 02 Apr 2003 11:33 PST
I cut and pasted the iframe into an aspx page. created a linkbutton.
went to the linkbutton click event and tried to create the
linkbutton1.navigateURL and and it (.navigateURL)dosn't exist as an
option and creates error if i force it.
So I have it working the other way I will await one last clarification
and close this answer. Since I do have it working the other way now, I
will leave it if I can't get the new way to work. if I decide I want
to make the new way work I will start another question again.

Thanks
Tony

Clarification of Answer by mmastrac-ga on 03 Apr 2003 10:31 PST
Ahh - I see why there is no NavigateURL property.  I was using the
HyperLink object, rather than the LinkButton object.  If you switch to
this object, it should work.  As it is working for you the other way,
this may not be necessary.

Good luck with your project!

Clarification of Answer by mmastrac-ga on 06 Apr 2003 10:53 PDT
Thanks for the tip!  Good luck in your project!

Matt.
tonymast-ga rated this answer:4 out of 5 stars and gave an additional tip of: $75.00
mmastrac-ga was patient and stuck with it until we came to a solution.
the final solution worked. thnak you for your time.

Comments  
Subject: Re: Create dynamic data and post to a server.
From: mmastrac-ga on 24 Mar 2003 12:16 PST
 
Try this instead:

req.ContentType = "application/x-www-form-urlencoded"

It turns out that the "Content-Type" header is marked as restricted,
meaning you need to access it through the appropriate property.

Please note that you will also need to add sw.Close() after
sw.WriteLine().  I have summarized the correct code here.  This code
uses a Windows.Forms app to run a test request against a CGI server
that echos what was sent.  This compiles and runs correctly for me.

--- Form1.vb ---
Imports System.Web
Imports System.Net
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form
Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim PostString As String
        PostString += "cmd=" + HttpUtility.UrlEncode("_cart")
        PostString += "&"
        PostString += "business=" + HttpUtility.UrlEncode("me@me.com")
        PostString += "&"
        PostString += "item_name=" + HttpUtility.UrlEncode("")
        PostString += "&"
        PostString += "item_number=" + HttpUtility.UrlEncode("")
        PostString += "&"
        PostString += "amount=" + HttpUtility.UrlEncode("")
        PostString += "&"
        PostString += "no_note=" + HttpUtility.UrlEncode("1")
        PostString += "&"
        PostString += "currency_code=" + HttpUtility.UrlEncode("USD")
        PostString += "&"
        PostString += "I1.x=" + HttpUtility.UrlEncode("1")
        PostString += "&"
        PostString += "I1.y=" + HttpUtility.UrlEncode("1")
        PostString += "&"
        PostString += "add=" + HttpUtility.UrlEncode("1")

        ' Create a new WebRequest  
        Dim req As HttpWebRequest =
WebRequest.Create("http://www.grack.com/cgi-bin/test")
        ' Make it a post  
        req.Method = "POST"
        req.ContentType = "application/x-www-form-encoded"
        req.ContentLength = PostString.Length
        ' Send the post data  
        Dim sw As StreamWriter = New
StreamWriter(req.GetRequestStream())
        sw.Write(PostString)
        sw.Close()
        ' Read the response  
        Dim sr As StreamReader = New
StreamReader(req.GetResponse().GetResponseStream())
        Dim ResponseText = sr.ReadToEnd()
        MsgBox(ResponseText)
    End Sub
End Class
Subject: Re: Create dynamic data and post to a server.
From: mmastrac-ga on 24 Mar 2003 12:18 PST
 
I can't seem to clarify my answer, but my last code had one last typo:
the Content-Type needs to be changed to:

application/x-www-form-urlencoded

The server may accept it as it is right now, but the above version is
the correct text.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy