How To Access Form Data With ImageButtons?

ImageButtons Have A Different Signature

Page 1 of 1

11 Replies - 1773 Views - Last Post: 12 November 2010 - 10:00 AM Rate Topic: -----

#1 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 09:25 AM

Paypal's Buy Now buttons use an ImageButton control which has a different signature than a regular ASP.NET button. I'm noticing that I can no longer access form data how I usually do because of this.

ImageButton:
<asp:ImageButton name="submit" border="0" ImageUrl="https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online" onclick="goPay" runat="server" />


Code I've tried:
Dim postData1 As String = hidden1.name + "=" + hidden1.value
Dim postData1 As String = form1.hidden1.name + "=" + form1.hidden1.value



Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: How To Access Form Data With ImageButtons?

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 10:31 AM

Can you post more code? Where is that code at? Do you have a HiddenField control on the form(I don't see the code for it)?

We need more than three lines of code. If the control is on the page, you can access it from the code behind. This isn't something special for .Net controls that is different from third-party controls. It's all the same.
Was This Post Helpful? 0
  • +
  • -

#3 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 10:56 AM

Form code:
    <form id="form1" runat="server">
    <div id="wrapper">
        <UserControl:header ID="Header1" runat="server" />
        <div id="content">
            <br />
            <br />
            <div align="center">
                <input id="hidden1" type="hidden" name="cmd" value="_xclick" />
                <input id="hidden2" type="hidden" name="business" value="...." />
                <input id="hidden3" type="hidden" name="item_name" value="Product 1" />
                <input id="hidden4" type="hidden" name="item_number" value="<% Response.Write(cID1) %>" />
                <input id="hidden5" type="hidden" name="amount" value="1" />
                <input id="hidden6" type="hidden" name="first_name" value="<% Response.Write(fname1) %>" />
                <input id="hidden7" type="hidden" name="last_name" value="<% Response.Write(lname1) %>" />
                <input id="hidden8" type="hidden" name="address1" value="<% Response.Write(address1) %>" />
                <input id="hidden9" type="hidden" name="city" value="<% Response.Write(city1) %>" />
                <input id="hidden10" type="hidden" name="state" value="<% Response.Write(state1) %>" />
                <input id="hidden11" type="hidden" name="zip" value="<% Response.Write(zip1) %>" />
                <input id="hidden12" type="hidden" name="night_phone_a" value="610" />
                <input id="hidden13" type="hidden" name="night_phone_b" value="555" />
                <input id="hidden14" type="hidden" name="night_phone_c" value="1234" />
                <input id="hidden15" type="hidden" name="email" value="<% Response.Write(email1) %>" />
                <input id="hidden16" type="hidden" name="notify_url" value="..../paypalResponse.aspx" />
                <input id="hidden17" type="hidden" name="return" value="...." />
                <asp:ImageButton name="submit" border="0" ImageUrl="https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online" onclick="goPay" runat="server" />
            </div>


goPay Sub:
Sub goPay(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
            Dim postData1 As String = hidden1.name + "=" + hidden1.value
            Dim postData2 As String = "&" + hidden2.name + "=" + hidden2.value
            Dim postData3 As String = "&" + hidden3.name + "=" + hidden3.value
            Dim postData4 As String = "&" + hidden4.name + "=" + hidden4.value
            Dim postData5 As String = "&" + hidden5.name + "=" + hidden5.value
            Dim postData6 As String = "&" + hidden6.name + "=" + hidden6.value
            Dim postData7 As String = "&" + hidden7.name + "=" + hidden7.value
            Dim postData8 As String = "&" + hidden8.name + "=" + hidden8.value
            Dim postData9 As String = "&" + hidden9.name + "=" + hidden9.value
            Dim postData10 As String = "&" + hidden10.name + "=" + hidden10.value
            Dim postData11 As String = "&" + hidden11.name + "=" + hidden11.value
            Dim postData12 As String = "&" + hidden12.name + "=" + hidden12.value
            Dim postData13 As String = "&" + hidden13.name + "=" + hidden13.value
            Dim postData14 As String = "&" + hidden14.name + "=" + hidden14.value
            Dim postData15 As String = "&" + hidden15.name + "=" + hidden15.value
            Dim postData16 As String = "&" + hidden16.name + "=" + hidden16.value
            Dim postData17 As String = "&" + hidden17.name + "=" + hidden17.value

            Dim ASCIIEncoding As Encoding = New ASCIIEncoding()
            Dim postData As String = postData1 + postData2 + postData3 + postData4 + postData5 + postData6 + postData7 + postData8 + postData9 + postData10 + postData11 + postData12 + postData13 + postData14 + postData15 + postData16 + postData17
            Dim data As Byte() = Encoding.GetBytes(postData)

            Dim myRequest As HttpWebRequest
            myRequest.Method = "POST"
            myRequest.ContentType = "application/x-www-form-urlencoded"
            myRequest.ContentLength = Data.Length
            Dim newStream As Stream = myRequest.GetRequestStream()
            newStream.Write(Data, 0, Data.Length)
            newStream.Close()

        End Sub


I'm also having an issue with line 22 from the goPay sub. I translated this from C# and I'm getting the error: reference to a non-shared member requires an object reference

I'm not getting much help from google on this one either.

This post has been edited by eTech1: 11 November 2010 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 11:09 AM

You need to add this attribute to ALL of the hidden input tags.

runat="server"



So your first hidden input would look like this...

<input id="hidden1" runat="server" type="hidden" name="cmd" value="_xclick" />



as for your issue with line 22, it seems in line 21, you are using "+" for concatenation. VB.Net uses an ampersand( & ) for concatenation.

Also, you need to change this line...

Dim ASCIIEncoding As Encoding = New ASCIIEncoding()



You are declaring a variable name that is the same as a Class name. This is bad form and can cause confusion(and possible build errors). Maybe change it to this...

Dim myEncoding As Encoding = New ASCIIEncoding()
.....
Dim data As Byte() = myEncoding.GetBytes(postData)


Was This Post Helpful? 0
  • +
  • -

#5 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 12:29 PM

I always forget the runat="server" and correct string concatenation so I should have caught those. It's always hard in ASP.NET because it doesn't show the concatenation line as having any problems.

I read the ASCCIIEncoding line incorrectly when translating from C#, I'm now using
Dim encoding As New ASCIIEncoding()


In between lines 24 and 25 I had to insert another line which I believe is the address of where you want the data to post
myRequest = DirectCast(WebRequest.Create("http://www.sandbox.paypal.com"), HttpWebRequest)


I have no errors but right now it is just posting back to the current page so I assume this last line is incorrect.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 12:42 PM

What are you expecting to happen? Your code simply writes to a stream. I am not sure what more you expect it to do.
Was This Post Helpful? 0
  • +
  • -

#7 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 12:47 PM

The example that I found said it would post the data to a web server that I specified. Is that not an accurate portrayal of what his example actually does? http://netomatix.com/HttpPostData.aspx
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 01:02 PM

Have you verified that the data is NOT getting to the Paypal server?
Was This Post Helpful? 0
  • +
  • -

#9 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 01:10 PM

I need the page to actually redirect my users to paypal with that post data being sent with it. Am I completely going in the wrong direction?
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 01:53 PM

View PosteTech1, on 11 November 2010 - 04:10 PM, said:

I need the page to actually redirect my users to paypal with that post data being sent with it. Am I completely going in the wrong direction?


I wouldn't say that you are going in the wrong direction, but I don't think you have a full understanding of how ASP.Net works. If you want to redirect to another page, then YOU have to do the redirection in code(for the purpose of what you are trying to do).

Response.Redirect("http://www.google.com")



Putting that at the end of your button click will redirect you to Google when the button is clicked.

Now, to redirect to the same page that you are writing the POST data to, I am not totally sure how to do that.

Try this link and see if that helps...

http://www.codeproje...ectAndPost.aspx
Was This Post Helpful? 0
  • +
  • -

#11 eTech1  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 135
  • Joined: 27-August 10

Re: How To Access Form Data With ImageButtons?

Posted 11 November 2010 - 02:40 PM

Here's what I'm getting from everything I've read:

Response.Redirect can't post data.
Server.Transfer can only post data to the server this page is on.
HttpWebRequest can only do stuff in the background(AJAX type things)

My only other option I found was to call a javascript function OnClientClick and set the form's action in that function. Then in the same function do a form.submit. This is correctly sending me to paypal now but instead of displaying all my correct information in paypal, it is showing things like <%response.Write(lname1) %>. Obviously the javascript is being called before that input element loads but I'm still not sure why this is. Is the page posting back after the click and before the javascript function runs?

Out of curiosity I threw in a UseSubmitbehavior="false" and it didn't change anything.
Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How To Access Form Data With ImageButtons?

Posted 12 November 2010 - 10:00 AM

View PosteTech1, on 11 November 2010 - 01:10 PM, said:

I need the page to actually redirect my users to paypal with that post data being sent with it. Am I completely going in the wrong direction?

If you need to send form data and do a redirect, then you need to use Javascript. Otherwise the only way to POST data using ASP.NET is using the HttpWebRequest.

Post Request To Another Web Site With Redirection
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1