0

Possible Duplicate:
Read Post Data submitted to ASP.Net Form

I have a google checkout "buy now" button, and am trying to add dynamically created content to send when it's clicked. Using the original html is proving a bit difficult so I want to create an ASP.Net ImageButton Instead of that.

I've succeeded in creating the button with the right image, and hooking it up to an event handler in the codebehind.

However, I'm not sure what exactly happens when the original button is clicked, in order to try and emulate it in the new ImageButton.

The original code is:

<form action="https://sandbox.google.com/checkout/..." id="Form1" method="post" name="..." target="_top">
<input name="item_name_1" type="hidden" value="..." />
...
<input alt="" src="https://sandbox.google.com/checkout/buttons/buy.gif?merchant_id=..." type="image" />
</form>

And I want to place a dynamically created item_name_1.

What do I have to do in the Button1_Click method for that?

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • @MarcusHansson Thanks. That is actually a useful page. But it only addresses the question of how to _read_ that stuff. What I need is to know how to _write_ it. – ispiro Jun 20 '12 at 13:50
  • Of course. This happens every time, read 80%, miss 90%. – Marcus Hansson Jun 20 '12 at 14:00
  • 1
    You should probably implement the google checkout API (https://developers.google.com/checkout/) in your code behind, but what do you mean by "dynamically created content"? If thats just a value of your product you include in the URL i.e. ?product=1234, rather than store it in a hiddenfield you can include it in the URL of the google checkout button http://url.com/store.aspx?product=<%: Product_ID %> and scrap the ImageButton – tutts Jun 20 '12 at 14:16
  • @rocky Thanks. But I've already downloaded that, and apparently it only has support for a "cart" and not for "buy now" buttons. As for `<%...%>` - unfortunately, my page has a masterpage and one cannot have two forms in one ASP.net... See an answer to another question of mine: http://stackoverflow.com/a/11119123/939213 . – ispiro Jun 20 '12 at 14:24
  • Have you looked at Google's sample .NET code? It may help. https://google-developers.appspot.com/checkout/samples/Google_Checkout_Sample_Code_NET#googleCheckoutSampleCodePrerequisites – Kevin Main Jun 20 '12 at 14:27
  • @KevinMain I have. see my answer to rocky. – ispiro Jun 20 '12 at 14:28
  • @ispiro if the page content is static (no postbacks) and the Form has the runat="server", you could always cast the form in the Masterpage and on Page_Load set the Form.Action to the google checkout? It might fall over if the checkout receives more than it expects though. Might be worth a shot. If that doesn't work you'll need to generate the page POST in the code behind, see http://stackoverflow.com/questions/2258864/redirect-to-another-page-using-post-method-from-code-behind – tutts Jun 20 '12 at 14:55
  • @rocky is correct, and this question is pretty much a duplicate of your [other SO post](http://stackoverflow.com/q/11118890/304683). "Cart" in technical terms of the API refers to the part where you send/submit of your data to Google. – EdSF Jun 20 '12 at 15:05
  • Duplicate? Not at all. As for "Cart" – I didn't know the word had a technical meaning. I was simply referring to their "Shopping Cart" which their API supports, as opposed to the "Buy Now" which it seems not to. – ispiro Jun 20 '12 at 20:26

4 Answers4

1

The short, concise and usefull version:

Html:

<form id="__parent" action="..." method="post" runat="server">
    <input id="__child0" name="type" type="hidden" value="button" runat="server" />
    <input id="__child1" name="name" type="hidden" value="teh_button" runat="server" />
    <input id="__child2" name="value" type="hidden" value="Hello?" runat="server" />
</form>

tehfile.cs:

<%@ Page Language="C#" CodeFile="tehfile.cs" %>

String
    _type = __child0.Value,
    _name = __child1.Value,
    _value     = __child2.Value,
    _element   = String.Format( 
        "<{0} {1}=\"{2}\" {3}=\"{4}\" {5}=\"{6}\" />", 
        "input", 
        "type", _type,
        "name", _name,
        "value", _value  );

Literal _lit = new Literal( );
_lit.Text = _element;

__parent.AddControl( _lit );
Marcus Hansson
  • 816
  • 2
  • 8
  • 17
  • Thanks again. But I fail to see how this addresses the question of how to use the codebehind to _send_ the information – you are sending it using html, (and not adding dynamic content) and only _reading_ it using C#. What I want is to have ASP.Net (C#) _sending_ the information. – ispiro Jun 20 '12 at 13:59
  • It should work out of the box. If not, well, I leave it as an excercise for you to figure the rest out (or you could pay me to put my work aside and learn asp.net right now, right here for you :) ). – Marcus Hansson Jun 20 '12 at 14:28
  • OK. Now I understand what you're saying. Thanks. – ispiro Jun 20 '12 at 20:35
  • Do note though, just as @EdSF wrote in his answer, this is *exactly* the same as the first version of my answer, with the only difference being that instead of accessing it through the post-/get-array, you're letting the framework encapsulate the data. – Marcus Hansson Jun 21 '12 at 09:35
  • Assuming you're referring to the first half of the code now (-until "_element ") - I don't need that part of the code at all, and that's why I didn't understand what you were trying to do there. What I need is how to POST. And you supplied me with the beginning of an answer to that with the idea to create a literal and add it to the form's controls. Thanks for that. (I'm still trying to find how to actually POST the page.) – ispiro Jun 21 '12 at 10:15
  • Clue: Look at the ID's in the html. `__parent` corresponds to the node with ID `__parent`. It's a [System.Web.UI.Control](http://msdn.microsoft.com/en-us/library/system.web.ui.control.aspx) (Or any type inheriting from it at least, dependent on the type of tag). – Marcus Hansson Jun 21 '12 at 13:25
1

To post that data to another server on the ASP.NET server-side, you are going to need to use something like the WebRequest class.

mccrager
  • 1,038
  • 6
  • 13
1

Or also in order to post a form, you can use a remote post class like any of the ones here: Remote HTTP Post with C# , the answer by @BobbyShaftoe is the one i've used in many projects.

Community
  • 1
  • 1
kolin
  • 2,326
  • 1
  • 28
  • 46
0

Same question/POST here. I would have commented instead of answered, but seems this is a better/formatted way to get to bottom of it all:

In your comment to @MarcusHansson's answer:

I fail to see how this addresses the question of how to use the codebehind to send the information

You are mixing server side with client side submission methods.

If you want to submit using "code behind" you must implement server-to-server HTTP Post. In the context of Google Checkout, I've provided that link in your other post.

Your client side is using an HTML FORM which in, and of itself is how you "send the data". You can try all sorts of client-side submission processes, but at the end of the day, it is a client-side (Javascript) method.

What is "dynamic" about your buy now button? It's meant for a single item (at a time) purchase. Why can't you construct all the variables you need at the same time you create the button? What are you adding (that requires another redirect or postback)?

Community
  • 1
  • 1
EdSF
  • 11,753
  • 6
  • 42
  • 83