?
Solved

How to automate creation of hidden fields ?

Posted on 2009-04-28
19
Medium Priority
?
592 Views
Last Modified: 2013-11-07
HI

In order to pay using paypal standard services they stablished as a requirements the following:

1. A form with POST action, pointing to a secured website.
2. hidden values per each shopping car item (Quantity, Item and Price).
3. hidden values for some configuration purposes.

Problem 1. How to dynamically create hidden values per each item ?
Problem 2. I am using master pages, what about forms and hidden values?

I have attached a code example:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="seller@designerfotos.com">
<input type="hidden" name="item_name_1" value="Item Name 1">
<input type="hidden" name="amount_1" value="1.00">
<input type="hidden" name="item_name_2" value="Item Name 2">
<input type="hidden" name="amount_2" value="2.00">
<input type="submit" value="PayPal">
</form>

Open in new window

0
Comment
Question by:dimensionav
  • 10
  • 9
19 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24256191
You can do this to create your first hidden element. You can do the same for any of them.

Dim hidElem As New HtmlInputHidden()hidElem.Name = "cmd"hidElem.ID = "cmd"hidElem.Value = "_cart"Me.Controls.Add(hidElem)
On your second question: you can do the same from a masterpage. A master page is not really a page, consider it a part of your aspx page. In the same way, the hidden values can be just as well used in masterpages as anywhere else.
0
 
LVL 39

Expert Comment

by:abel
ID: 24256200
Passing on a value or a property from the masterpage to the normal aspx page can be done using the technique using MasterType, which is pretty easy to do, see here for a simple tutorial: http://odetocode.com/Blogs/scott/archive/2005/07/16/1944.aspx
0
 

Author Comment

by:dimensionav
ID: 24268439
HI

I have attached two files, one with only HTML (wich works correctly) and other one wiht the new asp.net code and doesn´t work at all.

Regards.
This is Ok:
 
<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<script runat="server">
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
    <div>
        <input type="hidden" name="upload" value="1"/>
        <input type="hidden" name="business" value="dimensionav@gmail.com"/>
        <input type="hidden" name="item_name_1" value="Item Name 1"/>
        <input type="hidden" name="amount_1" value="1.00"/>
        <input type="submit" value="PayPal"/>
    </div>
    </form>
</body>
</html>
 
 
This is the new form that seems to be not posting the data
 
<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
 
<script runat="server">
   
    Dim hidElem As New HtmlInputHidden()
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        hidElem.Name = "cmd"
        hidElem.Value = "_cart"
        Me.Controls.Add(hidElem)
        hidElem.Name = "upload"
        hidElem.Value = "1"
        Me.Controls.Add(hidElem)
        hidElem.Name = "business"
        hidElem.Value = "dimensionav@gmail.com"
        Me.Controls.Add(hidElem)
        hidElem.Name = "item_name_1"
        hidElem.Value = "Item Name 1"
        Me.Controls.Add(hidElem)
        hidElem.Name = "amount_1"
        hidElem.Value = "2.00"
        Me.Controls.Add(hidElem)
    End Sub
    
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
    <div>
        <input type="submit" value="PayPal"/>
    </div>
    </form>
</body>
</html>

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 39

Expert Comment

by:abel
ID: 24276937
       hidElem.Name = "cmd"
        hidElem.Value = "_cart"
        Me.Controls.Add(hidElem)
        hidElem.Name = "upload"
        hidElem.Value = "1"
 
that part won't work, because hidElem is an object and you are actually just reassigning the properties of the same object (and object is not copied if you assign it or Add it, you just assign a pointer to the same object). By the end of your code, you will have one hidden element with the last properties "amount_1".

Try to do

  hidElem = New HtmlInputHidden()

after each Me.Controls.Add action. Then you should get it working.
0
 

Author Comment

by:dimensionav
ID: 24280296
abel:

I did what you said (what a dummy, ha), in any case paypal web site is still not receiving hidden fields, I believe this is because viewstate or something, What do you think?

I have attached the new code (both files were corrected) and some screen shots of what they are responding.

Is there a way to know if posts are correctly sent?

Thanks again.
THIS CODE IS OK:
________________
 
<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
 
<script runat="server">
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
    <div>
        <input type="hidden" name="cmd" value="_cart"/>
        <input type="hidden" name="upload" value="1"/>
        <input type="hidden" name="business" value="dimensionav@gmail.com"/>
        <input type="hidden" name="item_name_1" value="Item Name 1"/>
        <input type="hidden" name="amount_1" value="1.00"/>
        <input type="submit" value="PayPal"/>
    </div>
    </form>
</body>
</html>
 
 
THIS CODE IS GIVING THE ERRORS:
_______________________________
 
<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
 
<script runat="server">
   
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
 
        Dim hidElem As New HtmlInputHidden()
        'hidElem.ID = "cmd"
        hidElem.Name = "cmd"
        hidElem.Value = "_cart"
        hidElem.EnableViewState = False
        Me.Controls.Add(hidElem)
        hidElem = New HtmlInputHidden()
        'hidElem.ID = "upload"
        hidElem.Name = "upload"
        hidElem.Value = "1"
        hidElem.EnableViewState = False
        Me.Controls.Add(hidElem)
        hidElem = New HtmlInputHidden()
        'hidElem.ID = "business"
        hidElem.Name = "business"
        hidElem.Value = "dimensionav@gmail.com"
        hidElem.EnableViewState = False
        Me.Controls.Add(hidElem)
        hidElem = New HtmlInputHidden()
        'hidElem.ID = "item1"
        hidElem.Name = "item_name_1"
        hidElem.Value = "Item Name 1"
        hidElem.EnableViewState = False
        Me.Controls.Add(hidElem)
        hidElem = New HtmlInputHidden()
        'hidElem.ID = "amount1"
        hidElem.Name = "amount_1"
        hidElem.Value = "2.00"
        hidElem.EnableViewState = False
        Me.Controls.Add(hidElem)
        
    End Sub
    
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <div>
        <input type="submit" value="PayPal"/>
    </div>
    </form>
</body>
</html>

Open in new window

-Correct.jpg
-Error.jpg
0
 

Author Comment

by:dimensionav
ID: 24282454
I´ve found this: http://www.jigar.net/articles/viewhtmlcontent78.aspx, but I´m not so good at C#, do you have any idea how to implement it on VB ?

Thanks.
0
 

Author Comment

by:dimensionav
ID: 24286306
I´ve figured out how to do it.
    Public Sub RemotePost()
 
        Dim Url As String = "https://www.sandbox.paypal.com/cgi-bin/webscr"
        Dim Metodo As String = "post"
        Dim IdFormulario As String = "Form1"
 
        System.Web.HttpContext.Current.Response.Clear()
 
        System.Web.HttpContext.Current.Response.Write("<html><head>")
 
        System.Web.HttpContext.Current.Response.Write(String.Format("</head><body onload='document." & IdFormulario & ".submit()'>"))
        System.Web.HttpContext.Current.Response.Write(String.Format("<form name='" & IdFormulario & "' method='" & Metodo & "' action='" & Url & "' >"))
 
        'hidden Fields of Initial Configuration
        System.Web.HttpContext.Current.Response.Write(String.Format("<input type='hidden' name='cmd' value='_cart'/>"))
        System.Web.HttpContext.Current.Response.Write(String.Format("<input type='hidden' name='upload' value='1'/>"))
        System.Web.HttpContext.Current.Response.Write(String.Format("<input type='hidden' name='business' value='dimensionav@gmail.com'/>"))
 
        'Example of Shopping cart elements
        System.Web.HttpContext.Current.Response.Write(String.Format("<input type='hidden' name='item_name_1' value='Producto 1'/>"))
        System.Web.HttpContext.Current.Response.Write(String.Format("<input type='hidden' name='amount_1' value='1.00'/>"))
        System.Web.HttpContext.Current.Response.Write(String.Format("<input type='hidden' name='item_name_2' value='Producto 2'/>"))
        System.Web.HttpContext.Current.Response.Write(String.Format("<input type='hidden' name='amount_2' value='2.00'/>"))
 
 
        ' You might want to automate the previuous code with an IF getting elements from your own shopping cart
 
        System.Web.HttpContext.Current.Response.Write("</form>")
        System.Web.HttpContext.Current.Response.Write("</body></html>")
        System.Web.HttpContext.Current.Response.End()
 
    End Sub

Open in new window

0
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24293902
I'm glad you found out. I was away for a few days, so I missed the follow-ups. I can, however, explain you why this is going the way it was going (the paypal bit, I mean).

If you create elements by code (your original request) you can do that in basically only one way: the object oriented approach. The problem with this is, that you create elements that automatically have the "runat=server", which means: the ID of the elements will be set by  ASP.NET. To work with other sites, you need control over this ID (browsers send either the NAME or ID of an element, if both are present, they usually send the ID only, if you omit the ID, the ID is added by ASP.NET).

You chose the Response.Write method. This is possible (as you found out) but it is also tricky to get right. Now you have a page that is totally created by response.writes and is hard to debug because any mistakes in the HTML are easily overseen and you cannot add anything else to the website unless you do the response.write bit again for each element.

A slightly better approach is to use literal elements without the runat=server set. These attributes you will have full control over and you can place your code that sets the values inside <%=....%> blocks.

An even better approach is the original approach I proposed (I didn't know the paypal bit at the time) and to override the ID of the elements, which is nothing more then just setting the ID. Even the form ID and Name can be overridden in the same way and the ASP.NET engine will not change the ID + Name of the form once you set it.

In general: just make sure, always, that you provide an id and a name for each input element. You should do that in your code above as well, it will be better cross-browser friendly. The values for id and name should be the same.

Maybe this story has made it more understandable for you for what's going on. If so, you can ask me to interrupt the closing process and share the points (still sticking for your own answer, of course), if not, I won't object to the closing of the question (after all, you have done the major part yourself).

-- Abel --
0
 
LVL 39

Expert Comment

by:abel
ID: 24294142
And, what I forgot to add, you probably need to set the PostBackUrl on the button that's supposed to send your data to the PayPal website. You may have used a Redirect, which will not work (browsers don't allow posted data to be forwarded to another url, they only allow normal redirects,without postdata.
0
 

Author Comment

by:dimensionav
ID: 24297871
Abel:

Would you mind to give some example code about your solution, including the PostBackUrl event?.
Please, interrupt the closing process.

Thanks.
0
 

Author Comment

by:dimensionav
ID: 24310043
Abel I have objected this question.

Regards.
0
 
LVL 39

Assisted Solution

by:abel
abel earned 2000 total points
ID: 24310227
ah, thanks for that :)

The following ASPX code will create the correct output for you, and it shows the use of the postbackurl. You can also change the target at the form element, of course. In the Code Behind, I put the following lines, which changes both the name and the id of the hidden elements (if you change the ID, ASP.NET will automatically rename the Name attribute too).

item_name_1.ID = "item_name_2"
form1.ID = "FormX"
I set EnableEventValidation and EnableViewState to false, but these settings will not hamper the workings if you set them to true.

The output (part of it) looks like this, after the above code is put in the Page_Load event:

           <form name="FormX" method="post" action="Q24363774.aspx" id="FormX"><snip... />    <div>        <p>some hidden fields are hidden</p>        <input name="item_name_2" type="hidden" id="item_name_2" value="Producto 1" />        <input type="submit" name="testButton" value="send me somewhere" id="testButton" />    </div></form>
The important thing to see is: there is no fancy nomenclature going on about the names or ids of the elements, and you have all control over it the "normal way".

In the generated code I cut out the part of the PostBackUrl, which is a bit of JavaScript generated by ASP.NET, which will do the redirect of the POST data automatically. If you do not like the javascript approach, you can also change the target url of the <form> element.

-- Abel --
<%@ Page Language="C#" AutoEventWireup="true"
    EnableEventValidation="false"
    EnableViewState="false"
    CodeBehind="Q24363774.aspx.cs"
    Inherits="WebApplication1.EE_code.Q24363774" %>
 
<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
 
    <head>
        <title>test page with hidden fields</title>
    </head>
        
    <body>
        <form id="form1" runat="server">
        <div>
            <p>some hidden fields are hidden</p>
            <input runat="server" type='hidden'
                name='item_name_1' id="item_name_1"
                value='Producto 1' />
                
            <asp:Button runat="server" ID="testButton"
                PostBackUrl="http://example.com"
                Text="send me somewhere" />
        </div>
        </form>
    </body>
</html>

Open in new window

0
 

Author Comment

by:dimensionav
ID: 24310507
abel:

I´m sorry but I´m confused, I still don´t get how to dinamically create hidden fields.

Would be possible that you show me the HTML and the Codebehind (could be VB?) documents?
I believe that watching a complete example could be more understandable to me.
Thanks.
0
 
LVL 39

Assisted Solution

by:abel
abel earned 2000 total points
ID: 24311350
apologies, above showed a way to change the ID of input elements dynamically, not how to create a complete control, but the code was pretty complete, I didn't leave a single line out.

The below is for the page_load. It is the same as your earlier code, but this time, the elements are added to the <form> element with the name form1. The earlier Me.Controls.Add was not good: it added the controls to the bottom of the page, after the <from></form> code. Using this new code snippet, your original code should work without errors:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
 
        Dim hidElem As New HtmlInputHidden()
        hidElem.ID = "cmd"
        hidElem.Name = "cmd"
        hidElem.Value = "_cart"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
 
        hidElem = New HtmlInputHidden()
        hidElem.ID = "upload"
        hidElem.Name = "upload"
        hidElem.Value = "1"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
 
        hidElem = New HtmlInputHidden()
        hidElem.ID = "business"
        hidElem.Name = "business"
        hidElem.Value = "dimensionav@gmail.com"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
 
        hidElem = New HtmlInputHidden()
        hidElem.ID = "item1"
        hidElem.Name = "item_name_1"
        hidElem.Value = "Item Name 1"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
 
        hidElem = New HtmlInputHidden()
        hidElem.ID = "amount1"
        hidElem.Name = "amount_1"
        hidElem.Value = "2.00"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
        
    End Sub

Open in new window

0
 

Author Comment

by:dimensionav
ID: 24315766
Abel:

Ok, now I got it, so let me tell you that every time that my code includes the ID per each control I got the "3005 error from paypal", I have tried this before, if you see my previous code I let ID property commented.

I´m sure that something is wrong with my code.
I really appreciate your patience.
<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
 
        Dim hidElem As New HtmlInputHidden()
        hidElem.ID = "cmd"
        hidElem.Name = "cmd"
        hidElem.Value = "_cart"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
 
        hidElem = New HtmlInputHidden()
        hidElem.ID = "upload"
        hidElem.Name = "upload"
        hidElem.Value = "1"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
 
        hidElem = New HtmlInputHidden()
        hidElem.ID = "business"
        hidElem.Name = "business"
        hidElem.Value = "dimensionav@gmail.com"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
 
        hidElem = New HtmlInputHidden()
        hidElem.ID = "item1"
        hidElem.Name = "item_name_1"
        hidElem.Value = "Item Name 1"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
 
        hidElem = New HtmlInputHidden()
        hidElem.ID = "amount1"
        hidElem.Name = "amount_1"
        hidElem.Value = "2.00"
        hidElem.EnableViewState = False
        form1.Controls.Add(hidElem)
        
    End Sub
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
        <form id="form1" runat="server" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
        <div>
        <input type="submit" value="PayPal"/>
        </div>
        </form>
 
</body>
</html>

Open in new window

-008.jpg
0
 
LVL 39

Assisted Solution

by:abel
abel earned 2000 total points
ID: 24316031
That is probably because there are discrepancies in your code. the .Name and the .ID must be equal. However, by only providing the .ID, you can skip the .Name, because .NET will automatically add that for you. One example from your own code above that needs to be fixed:

       hidElem.ID = "amount1"        hidElem.Name = "amount_1"
fixed could be the following, depending one what name it should be:
       
hidElem.ID = "amount_1"         hidElem.Name = "amount_1"
or such:

       hidElem.ID = "amount_1"
       
You may want to put the code for creating the hidden element inside a function, which can prevent such errors.


0
 

Author Comment

by:dimensionav
ID: 24319108
It worked, I really thank you very much.

A final question Abel:
The idea of using postbackUrl is that this button could be sent as a part of the form in order to return to my site after paying on paypal?

Thanks.
0
 
LVL 39

Expert Comment

by:abel
ID: 24319254
no. All it does is sending the form and everything on it to another server or url on your own server. Whether it "returns" after some actions back to you is entirely up to the implemenation on that other server. If paypal has a way to get the customer back to your server after the payment process is done (and I'm sure they have) then you should follow that way. PostbankUrl is just a convenience way to get the post-data to them, and nothing more.
0
 

Author Closing Comment

by:dimensionav
ID: 31575719
Thank You Very much
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question