Microsoft KB Archive/311294

= How to host a WebBrowser control in Visual Basic .NET to post form data =

Article ID: 311294

Article Last Modified on 5/21/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q311294





For a Microsoft Visual C# .NET version of this article, see 313068.



For a Microsoft Visual Basic 6.0 version of this article, see 256195.



IN THIS TASK

 * SUMMARY
 * Create an .aspx page
 * Create the Visual Basic .NET sample project
 * REFERENCES



SUMMARY
This article discusses how to use the WebBrowser ActiveX control in Microsoft Visual Basic .NET to post form data. In Visual Basic .NET, you can use the POST method with the WebBrowser ActiveX control to send data to an HTTP server such as Microsoft Internet Information Server (IIS).

To post data, you can use the Navigate method or the Navigate2 method of the WebBrowser ActiveX control. In these methods, only the URL parameter, the PostData parameter, and the Headers parameter are relevant.

To call the Navigate method to post form data to an HTTP server, make sure that the parameters include the following information:  The URL parameter must specify a valid address. The PostData parameter must contain an array of bytes.  The Headers parameter must contain the following HTTP header. Content-Type: application/x-www-form-urlencoded This header indicates that the data that is posted is encoded according to the HTML specification. 

Note The Microsoft Internet Explorer Script Object Model has a Window object. This Window object also has a Navigate method. However, the Navigate method of this Window object only accepts a URL. Also, you cannot use this Navigate method to post data to a Web server.

back to the top

Create an .aspx page
 Create a new Microsoft ASP.NET (.aspx) Web Application project. By default, the WebForm1.aspx page is created.</li>  In the WebForm1.aspx code behind page that contains HTML tags, replace the existing code with the following code. <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;WebApplication4.WebForm1&quot;%> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> <TITLE>NAVPOST PAGE</TITLE> </HEAD> <%  Dim cFlavor as string = Request(&quot;Flavor&quot;) Dim cName as String = Request(&quot;FName&quot;) Response.Write( &quot;Hello&quot; & cName & &quot;, &quot; & &quot;One scoop of&quot; & cFlavor & &quot;is coming right up!&quot;) %> </HTML> </li> Save WebForm1.aspx.</li> On the Build menu, click Build Solution.</li></ol>

back to the top

Create the Visual Basic .NET sample project
To demonstrate a POST transaction in Visual Basic .NET, follow these steps: <ol> Use Visual Basic .NET to create a new Windows Application project. By default, a Windows Form that is named Form1 is created.</li> Add two Label controls, one Button control, one ComboBox control, and one TextBox control to the Form1 Windows Form.</li> Modify the Name property and the Text property of each control. Use the settings in the following table. </li> On the View menu, click Toolbox.</li> Right-click the Toolbox, and then click Add/Remove Items. The Customize Toolbox dialog box appears.</li> On the COM Components tab in the Customize Toolbox dialog box, add Microsoft WebBrowser (SHDOCVW.DLL) to the project.</li> Add a WebBrowser ActiveX control to the Windows Form. By default, the AxWebBrowser1 is added to the Windows Form.</li>  To import the System.Text namespace into the project, add the following code at the beginning of the Form1.vb file. Imports System.Text </li>  Add the following code to the Form1.vb file. Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load

cboFlavor.Items.Add(&quot;Vanilla&quot;) cboFlavor.Items.Add(&quot;Chocolate&quot;) cboFlavor.Items.Add(&quot;Strawberry&quot;) cboFlavor.SelectedIndex = 0

End Sub

Private Sub cmdSubmit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdSubmit.Click Dim vPost As Object Dim vHeaders As Object Dim cFlavor As String Dim cParamFlavor As String Dim cParamName As String Dim cPostData As String Dim cSeparator As String

cFlavor = cboFlavor.SelectedItem cParamFlavor = &quot;Flavor=&quot; cSeparator = &quot;&&quot; cParamName = &quot;FName=&quot; cPostData = cParamName & txtBoxName.Text & cSeparator & cParamFlavor & cFlavor vHeaders = &quot;Content-Type: application/x-www-form-urlencoded&quot; + Chr(10) + Chr(13) vPost = ASCIIEncoding.ASCII.GetBytes(cPostData) AxWebBrowser1.Navigate2(&quot;http:// /WebForm1.aspx&quot;, Nothing, Nothing, vPost, vHeaders)

End Sub The ASCIIEncoding class provides a method to convert a string into an array of bytes. </li> Modify the URL in the call to the Navigate2 method. Use a URL that is correct for your project.</li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start to run the application.</li> In the First Name box, type your name.</li> <li>Select a flavor, and then click Submit. Notice that the data from the Windows Form is posted to the HTTP server. Additionally, the response appears in the browser.</li></ol>

back to the top

<div class="references_section">