Add form objects in code?

D

DOUGLAS HEESTAND

How do you add a textbox in code and add them to a particular form? I
am using WebRequest.Create (vb.net) to send a form request to
authorize.net for credit card processing. They require that one of
the fields is the account password. I have to send that as part of my
form but I don't want to have it visible to the user so a hidden form
field won't work. Is there a way to create the control in code, in
the proper form, before I send the form? Here is my code for sending
the form (it is attached to the onClick event for my submit button).

Thanks in advance!

---------------
Code
---------------
Dim objRequest As HttpWebRequest
Dim strPost As String
Dim strRequest As String
Dim strResult As String
Dim arrRequest As Byte()
Dim objUTF8Encoding As UTF8Encoding
Dim strmRequest As Stream
Dim objResponse As HttpWebResponse
Dim srResponse As StreamReader

'####INSERT CODE TO CREATE PASSWORD TEXTBOX HERE##########

strPost = Request.Form().ToString()

objRequest = CType(WebRequest.Create("https://secure.authorize.net/gateway/transact.dll"),
HttpWebRequest)
objRequest.Method = "POST"
objRequest.ContentType = "application/x-www-form-urlencoded"
strRequest = strPost
objUTF8Encoding = New UTF8Encoding
arrRequest = objUTF8Encoding.GetBytes(strRequest)
objRequest.ContentLength = strPost.Length
strmRequest = objRequest.GetRequestStream()
strmRequest.Write(arrRequest, 0, arrRequest.Length)
strmRequest.Close()

objResponse = objRequest.GetResponse()
srResponse = New StreamReader(objResponse.GetResponseStream(),
Encoding.ASCII)
strResult = srResponse.ReadToEnd()
 
K

Ken Cox [Microsoft MVP]

Hi Douglas,

Another approach is to use the WebClient class. It lets you add anything you
want to the upload values collection. You could get some parts from the form
and add your own.

Sub DoPost()
Dim uriString As String = _
"http://localhost/p4320work/receiver.aspx"
' Create a new WebClient instance.
Dim myWebClient As New WebClient
' Create a new NameValueCollection instance to hold
'some custom parameters to be posted to the URL.
Dim myNameValueCollection As New NameValueCollection
Dim name As String = ""
name = TextBox1.Text
Console.Write("Age:")
Dim age As String
age = TextBox2.Text
Dim privatedata As String
privatedata = "mysecretstuff"
' Add necessary parameter/value
'pairs to the name/value container.
myNameValueCollection.Add("Name", name)
myNameValueCollection.Add("Age", age)
myNameValueCollection.Add("Private", privatedata)
' Upload the NameValueCollection.
Dim responseArray As Byte() = myWebClient.UploadValues _
(uriString, "POST", myNameValueCollection)
' Decode and display the response.
Label1.Text = "Response received was :" & _
Encoding.ASCII.GetString(responseArray)
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]
Toronto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top