How to host a WebBrowser control in Visual Basic .NET to post form data (311294)
The information in this article applies to:
- Microsoft Visual Basic .NET (2002)
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.
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:
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 topCreate an .aspx page- Create a new Microsoft ASP.NET (.aspx) Web Application project. By default, the WebForm1.aspx page is created.
- In the WebForm1.aspx code behind page that contains HTML tags, replace the existing code with the following code.
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication4.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>NAVPOST PAGE</TITLE>
</HEAD>
<%
Dim cFlavor as string = Request("Flavor")
Dim cName as String = Request("FName")
Response.Write( "Hello" & cName & ",<br>" & "One scoop of" & cFlavor & "is coming right up!")
%>
</HTML>
- Save WebForm1.aspx.
- On the Build menu, click Build Solution.
back to the topCreate the Visual Basic .NET sample project To demonstrate a POST transaction in Visual Basic .NET, follow
these steps:
- Use Visual Basic .NET to create a new Windows Application project.
By default, a Windows Form that is named Form1 is created.
- Add two Label controls, one Button control, one ComboBox control, and one TextBox control to the Form1 Windows Form.
- Modify the Name property and the Text property of each control. Use the settings in the following table.
|
Label | lblName | First Name | Label | lblFlavor | Flavor | Button | cmdSubmit | Submit | ComboBox | cboFlavor | | TextBox | txtBoxName | |
- On the View menu, click Toolbox.
- Right-click the Toolbox, and then click Add/Remove Items. The Customize Toolbox dialog box appears.
- On the COM Components tab in the Customize Toolbox dialog box, add Microsoft WebBrowser (SHDOCVW.DLL) to the
project.
- Add a WebBrowser ActiveX control to the Windows Form. By default, the AxWebBrowser1 is
added to the Windows Form.
- To import the System.Text namespace into the project, add the following code at the beginning of the Form1.vb file.
Imports System.Text
- 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("Vanilla")
cboFlavor.Items.Add("Chocolate")
cboFlavor.Items.Add("Strawberry")
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 = "Flavor="
cSeparator = "&"
cParamName = "FName="
cPostData = cParamName & txtBoxName.Text & cSeparator & cParamFlavor & cFlavor
vHeaders = "Content-Type: application/x-www-form-urlencoded" + Chr(10) + Chr(13)
vPost = ASCIIEncoding.ASCII.GetBytes(cPostData)
AxWebBrowser1.Navigate2("http://<server>/WebForm1.aspx", Nothing, Nothing, vPost, vHeaders)
End Sub
The ASCIIEncoding class provides a method to convert a string into an array of
bytes. - Modify the URL in the call to the Navigate2 method. Use a URL that is correct for your project.
- On the Build menu, click Build Solution.
- On the Debug menu, click Start to run the application.
- In the First Name box, type your name.
- 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.
back to the topREFERENCES
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
174923
How to use the PostData parameter in WebBrowser control
For more information about other parameters of the Navigate2 method, visit the following Microsoft Developer Network (MSDN) Web site: For more information about the WebBrowser ActiveX control, and about the
methods, the properties, and the events that the WebBrowser ActiveX control exposes,
visit the following MSDN Web site: For more information about the HTML specification for form
content types, visit the following World Wide Web Consortium (W3C) Web site:
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:
back to the top
Modification Type: | Major | Last Reviewed: | 4/21/2006 |
---|
Keywords: | kbHOWTOmaster kbWebBrowser KB311294 kbAudDeveloper |
---|
|