4 Replies - 49618 Views - Last Post: 06 May 2010 - 06:24 AM Rate Topic: -----

#1 mcguirestick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-August 07

VB.Net 2005 Webbrowser control Input/Submit Data

Post icon  Posted 15 August 2007 - 11:40 AM

Hi i have a webpage that uses the following code. It has two input boxs and a submit button.
<html>
<head><title>Vehicle movement log</title></head>
<body bgcolour='#00ffff'>

<LINK REL=stylesheet TYPE='text/css' HREF='vehiclelog.css' TITLE='style'>


<form name='dateandcad' action="default.asp" method="post" autocomplete="off">
<table border="0">
<tr><td width="220">Date (ddmmyy): <input type=text name=date size=8></td>
<td width="300">Call sign or fleet number: <input type=text name=callsign size=5></td>
<td width="100">  <input type="submit" value="submit"></td>
</table>
</form>


	<hr><br><br>


<script language='javascript'>
	dateandcad.date.value=''
	dateandcad.callsign.value=''
</script>

</body>
</html>


As an example. What i want to do is within VB input the data "160707" in the date input box and "QW12" in the callsign box and then click the submit box all in the code.
Please help
Thank you
Terry :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: VB.Net 2005 Webbrowser control Input/Submit Data

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: VB.Net 2005 Webbrowser control Input/Submit Data

Posted 15 August 2007 - 03:32 PM

First, use

CODE BLOCKS FOR YOUR CODE!



Please :)

Second, this is an ASP.Net question (from the looks of it) not VB.Net so its posted in the wrong forum (Ill get someone to move it, please don't double post :) )

Third, I'm not quite sure I completely understand what you're trying to do. Can I get a clearer explanation please :)
Was This Post Helpful? 0
  • +
  • -

#3 AbtZorba  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 31-August 07

Re: VB.Net 2005 Webbrowser control Input/Submit Data

Posted 31 August 2007 - 02:09 AM

View PostPsychoCoder, on 15 Aug, 2007 - 03:32 PM, said:

First, use

CODE BLOCKS FOR YOUR CODE!



Please :)

Second, this is an ASP.Net question (from the looks of it) not VB.Net so its posted in the wrong forum (Ill get someone to move it, please don't double post :) )

Third, I'm not quite sure I completely understand what you're trying to do. Can I get a clearer explanation please :)



I think the question is unclear indeed, but if i get it right it's not a asp.net question, but a VB.net question indeed.

If i'm right, this is the same challenge i face atm. I think mcguirestick wants to use a webbrowser control in vb.net, load the given page into the webbrowser control and then fill in the input fields on the form and also post the form programmatically.

I can help with what i have atm : entering data into the input fields and with the SendKeys method fake a stroke onto the enter key after focussing on the send button, which will cause the form to post.

Below is the code i use atm :

Dim htmle As HtmlElement
'loop over all 'input' fields in the form, 'Browser' is a webbrowser control
For Each htmle In Browser.document.GetElementsByTagName("Input")
Select Case htmle.Name
Case "date"
'enter some data into the input field
htmle.InnerText = "160707"
Case "callsign"
htmle.InnerText = "QW12"
Case "sendbutton" 'assuming this is the name of the send button
'set the focus on the button element
htmle.focus
'sending an "enter" key to the active application
SendKeys.SendWait(Chr(13))
End Select
Next

I'm not happy at all with my workaround to get the form posted. Using the sendkeys method requires the browser object to be visible on the display and also the sendkeys function sends the key to the active application, so you need to keep your application active all the time :S (or write some code to switch to your app before sending the 'enter' key)

if you're in control over the webpage's layout, you could do the post through a (java)script (which is usually fired 'onmousedown') and then call the javascript function from within your vb.net app. this you do with the folowing code :

'Browser' is a webbrowser control, the javascript function is named 'btm1'
Browser.document.InvokeScript("btm1")

in case your script would be in need of params, you just make an array of objects and pass it along :

'suppose we have 4 params
Dim objArray(4) As Object
objArray(0) = "s1"
objArray(1) = "p"
objArray(2) = "img/nl/b/ok2.gif"
objArray(3) = 1
Browser.document.InvokeScript("btm1", objArray)

Last but not least, you can just read all input tags using the for loop above and create the post string, then navigate to that address.

Assuming the form method is 'POST' :

Dim poststring as string = "default.asp?"
Dim htmle As HtmlElement
'loop over all 'input' fields in the form, 'Browser' is a webbrowser control
For Each htmle In Browser.document.GetElementsByTagName("Input")
Select Case htmle.Name
Case "date"
'enter some data into the input field
htmle.InnerText = "160707"
Case "callsign"
htmle.InnerText = "QW12"
End Select
poststring = poststring & htmle.Name & "=" & htmle.InnerText & "&"
Next
'remove either '?' (no input fields) or "&" at the end
poststring = poststring.substring(0, poststring.length-1)
'navigating to
Browser.Navigate(poststring)


I'm still looking for a way to do the post as if i were clicking the 'send' button, so if any of you can help me, plz do so!
Was This Post Helpful? 1

#7 bkwdesign  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 07-August 08

Re: VB.Net 2005 Webbrowser control Input/Submit Data

Posted 07 August 2008 - 07:52 AM

What you really want to do, is perform the POST request using System.Net.WebClient, get a response from the server (will come back as a byte array) convert it to text (usually it's HTML text that comes back) and show that HTML in the aforementioned WebBrowser control.

I haven't debugged this yet, but this is definitely the easiest way to attain what you're shooting for.

Public Sub PerformPost(ByVal destinationURL As System.Uri, ByVal postData As String)
		' use a System.Net.WebClient to perform a post programmatically
		' convert the response you get back to text and render in a web browser control
		' postData is in this format: this=value1&that=value2&other=value3
		Dim WebClient1 As New System.Net.WebClient()
		Dim encoding As New System.Text.ASCIIEncoding
		Dim response As Byte()

		response = WebClient1.UploadData(destinationURL, "POST", encoding.GetBytes(postData))

		WebBrowser1.DocumentText = encoding.GetString(response)

	End Sub




EDIT:
I originally posted code for setting headers explicitly, but turns out I did not need this block of code:
		WebClient1.Headers.Set(Net.HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
		WebClient1.Headers.Set(Net.HttpRequestHeader.ContentLength, postData.Length.ToString)
		WebClient1.Headers.Set(Net.HttpRequestHeader.ContentType, "application/x-www-form-urlencoded")


Also, while this method DOES work, the web browser control may treat the page as a local document and not find all the relative images, CSS and scripts unless you modify the HTML you get back as a response, and add a <BASE HREF="...."/> tag, or some similiar hack.

This post has been edited by bkwdesign: 07 August 2008 - 08:34 AM

Was This Post Helpful? 1

#8 Guest_arsal.2004.1@gmail.com*


Reputation:

Re: VB.Net 2005 Webbrowser control Input/Submit Data

Posted 06 May 2010 - 06:24 AM

WebBrowser1.document.All("submit").InvokeMember("click")

for submitting the form, or clicking the button...

you need to enter the name of the input button where i wrote submit...

and then Invoke its Click method... :)

hope it helpz
Was This Post Helpful? 0

Page 1 of 1