Microsoft KB Archive/311294

From BetaArchive Wiki

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.

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

  1. Create a new Microsoft ASP.NET (.aspx) Web Application project. By default, the WebForm1.aspx page is created.
  2. 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>
                        
  3. Save WebForm1.aspx.
  4. On the Build menu, click Build Solution.

back to the top

Create the Visual Basic .NET sample project

To demonstrate a POST transaction in Visual Basic .NET, follow these steps:

  1. Use Visual Basic .NET to create a new Windows Application project. By default, a Windows Form that is named Form1 is created.
  2. Add two Label controls, one Button control, one ComboBox control, and one TextBox control to the Form1 Windows Form.
  3. Modify the Name property and the Text property of each control. Use the settings in the following table.
    Control Name Text
    Label lblName First Name
    Label lblFlavor Flavor
    Button cmdSubmit Submit
    ComboBox cboFlavor
    TextBox txtBoxName
  4. On the View menu, click Toolbox.
  5. Right-click the Toolbox, and then click Add/Remove Items. The Customize Toolbox dialog box appears.
  6. On the COM Components tab in the Customize Toolbox dialog box, add Microsoft WebBrowser (SHDOCVW.DLL) to the project.
  7. Add a WebBrowser ActiveX control to the Windows Form. By default, the AxWebBrowser1 is added to the Windows Form.
  8. To import the System.Text namespace into the project, add the following code at the beginning of the Form1.vb file.

    Imports System.Text
                        
  9. 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.

  10. Modify the URL in the call to the Navigate2 method. Use a URL that is correct for your project.
  11. On the Build menu, click Build Solution.
  12. On the Debug menu, click Start to run the application.
  13. In the First Name box, type your name.
  14. 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 top

REFERENCES

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 site:

back to the top


Additional query words: safearray web browser

Keywords: kbhowtomaster kbwebbrowser KB311294