How to POST from WebForms to External URL and Supply Values from Server - asp.net

Having lived in an MVC world for the last few years I just cannot get my head back into a WebForms world.
I have to try to integrate an external payment getway into a 4 step process. The last step involves POSTing some values to a given URL. This URL is expecting a preset list of values with predefined names (i.e. I cannot change what it is expecting).
The problem I am having is populating values gained from steps 1-3 on the form I want to post.
Let's say I need to send 3 values (there are more but the principle is the same):
- amount
- returnurl
- name
I could write a form along the lines of
<form action="http://url/goes/here">
<p>Please confirm the details below</p>
<asp:TextBox id="amount" runat="server" />
<asp:TextBox id="returnurl" runat="server" />
<asp:TextBox id="name" runat="server" />
<asp:Button id="submitme" />
</form>
And easily pre-populate the textboxes (or labels, or whatever I need) from the codebehind on PageLoad.
But then the values are submitted with prefixed with the 'ctl00$ContentBottom$WebFormControl$ctl00$' and so the external url throws it away and complains no values have been supplied. It sees 'ctl00$ContentBottom$WebFormControl$ctl00$amount' and not 'amount'.
Is there anyway I can amend the names of the values that are posted?

instead of amending the names of value you can add them in your aspx page as follows
<input type="hidden" name="billing_cust_name" value="<%=txtbox1.Text%>">
<input type="hidden" name="billing_cust_address" value="<%=txtbox2.Text%>" >
<input type="hidden" name="billing_cust_email" value="<%=txtbox3.Text%>">
<input type="hidden" name="billing_cust_notes" value="<%=txtbox4.Text%>">
When the form is ready to be posted change the value and then post.

Related

ASP.NET WebForms form not being posted - why?

Although I don't consider myself a web programmer, I've done a fair amount of web programming, so I'm almost embarrassed to ask what is wrong with the below code. There is something fundamental about ASP.NET that I must be missing.
I have two pages, source.aspx and destination.aspx:
source.aspx - html:
<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
<input id="Text1" type="text" />
<input id="Text2" type="text" />
<input id="Checkbox1" type="checkbox" />
<input id="Submit1" type="submit" value="submit" />
</form>
</body>
destination.aspx - code behind:
protected void Page_Load(object sender, EventArgs e)
{
// Below variable gets assigned null.
string text1 = Request.Form["Text1"];
}
When I submit the source.aspx form, once it gets to the destination.aspx form, there is no information in the FORM variables. I thought that the forms 'runat="server" ' would ensure that I ended up in the ASP.NET page pipeline, and in fact I can step through this code. There are no POSTed form variables other than viewstate, and the PARAMs collection doesn't have anything corresponding to control data either, not even ones that would correspond to decorated control names. The question is, what is happening that is making my POSTed variables 'disappear', at least to the destination page?
I'm not looking for alternatives how to make this work (i.e. make the controls server controls with runat="server", etc). I can fix the problem. What I am trying to determine is 'what is it about ASP.NET that makes my controls not appear to be receivable by the destination page. Thanks - I thought that I understood HTTP pretty well, but there seems to be a little sleight of hand courtesy of ASP.NET that I'm not seeing.
You can remove the runat="server" off of your form tag since you want to opt out of what ASP.NET gives you with server controls. You are basically thinking correctly that this should work without needing all the ASP.NET page processing bits.
It's a small change you need to make - you need to use 'name' instead of 'id' on your input controls in order for them to appear in the Form collection. It's a subtle thing but I believe it's not exclusive to ASP.NET - the name attribute specifies what to associate the value with in the POST variable collection.
Consult HTML input - name vs. id for more information on id vs. name
Good luck
Request.Form uses name attribute from elements. So you should write name attributes to each of the html elements.
<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
<input id="Text1" name="Text1" type="text" />
<input id="Text2" name="Text2" type="text" />
<input id="Checkbox1" name="Checkbox1" type="checkbox" />
<input id="Submit1" type="submit" value="submit" />
</form>
</body>

Control.UniqueID different after cross-page postback

I have a simple ASP.NET page with a MasterPage. Within the MasterPage, I have two login fields:
<input type="text" runat="server" id="txtUserName"/>
<input type="text" runat="server" id="txtPassword"/>
When the controls are rendered to the page, ASP.NET renders the following:
<input type="text" runat="server" id="ctl00_txtUserName" name="ctl00$txtUserName"/>
<input type="text" runat="server" id="ctl00_txtPassword" name="ctl00$txtPassword"/>
If I understand correctly, the name attribute corresponds to the UniqueID property of a control. However, when I'm debugging Page_Load and attempt to view the UniqueID of these fields, they have different values (ctl0$txtUserName and ctl0$txtPassword respectively)!
Note that this does not seem to be an issue on all pages using this MasterPage. Most of them work correctly and use ctl0$txtUserName and ctl0$txtPassword in both rendering and Page_Load.
Any idea what might cause ASP.NET to render a different UniqueID for a control than it uses in Page_Load?
I'm still not sure what was causing the generated UniqueIDs in the MasterPage to be different in Page_Load than when rendered to the page. However, I was able to get around the issue by storing the UniqueIDs of these fields in hidden fields. I was then able to access the values directly in the Request.Form collection.
In other words, I did this:
In the MasterPage -
<input type="text" runat="server" id="txtUserName"/>
<input type="text" runat="server" id="txtPassword"/>
<input type="hidden" id="txtUserNameUID" value="<%=txtUserName.UniqueID%>"/>
<input type="hidden" id="txtPasswordUID" value="<%=txtPassword.UniqueID%>"/>
During Page_Load of the child page -
string username = Request.Form[Request.Form["txtUserNameUID"]];
string password = Request.Form[Request.Form["txtPasswordUID"]];
Hope this helps anyone else struggling with UniqueID weirdness in ASP.NET!
Weird quirk I just became aware of: any wrapping controls that are runat server must also have IDs. For instance, if you have a panel around the control, i.e. whatever "ctl00" is, it must be assigned an ID. If it is not set, it will be allocated one and this can change.

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

This question already has answers here:
Closed 10 years ago.
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?
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 );
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.
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.
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)?

Cross-page posting. Is it a good pratice to use PreviousPage in Asp.net?

I found this article on MSDN talking about Cross-page posting.
I never worked with Page.PreviousPage. But I think its interesting.
Do you use it? Is it a good pratice, or a bad idea?
What do you think about?
Thanks.
The cross page posting is a helper to post some data to a different page and still have the asp.net code behind functionality.
Why is this exist ? because asp.net have a limitation of one and only form per page. But actually to an html page you can have many forms and many different post to different pages.
So to give a tool to that case, is let you set a second page to post the data, and you setup this on the Button (and not by placing second form), and from there is solve this issue, to post the data to a different page.
For example... with out asp.net and with simple html on a page you can do that.
<body>
<form method="post" action="samepage.html">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>
<form method="post" action="page_b.html">
email for news letter: <input type="text" name="email" />
<input type="submit" value="Submit" />
</form>
</body>
To solve a situation like this, and because asp.net not allow two forms at the same page, gives this option.
<body>
<form id="form1" runat="server">
Username: <asp:TextBox runat="server" ID="Name" />
<asp:Button runat="server"/>
email for news letter: <asp:TextBox runat="server" ID="email" />
<asp:Button runat="server" PostBackUrl="page_b.aspx" />
</form>
</body>
In the second case, you have one form, but you set the PostBackUrl to a different page, and from there asp.net still handle the data on code behind direct on a second page (with out redirect).
I hope this example gives you and an idea where to really use the previous page. Also what is more usually is the Redirect, how ever there are case that you need to have the result to a different page. So its per case if you use it or not.

Post data to another aspx page

I have simple aspx page that has a form and a input field in it something like below.
//basket.aspx
<input type="text" id="TotalPrice" runat="server" name="TotalPrice" value="100" />
<asp:button ID="btnBuy" runat="server" Text="make payment" PostBackUrl="~/payment" />
</form>
This is going to payment.aspx but when I debug the Request.Form in the page load of the payment.aspx, I cannot reach the TotalPrice value like Request.Form["TotalPrice"]. What is the best way to make a post to another aspx page to get values ? Why the way I am trying to does not work ?
You should be using PreviousPage property on the target page.
Here is an article on how to make cross post backs work

Resources