What happens to an “input” on “submit”? And how can that be done by ASP.Net? [duplicate]

二次信任 提交于 2019-12-11 18:57:34

问题


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?


回答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 );



回答2:


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.




回答3:


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.




回答4:


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)?



来源:https://stackoverflow.com/questions/11120911/what-happens-to-an-input-on-submit-and-how-can-that-be-done-by-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!