Programming Microsoft Web Forms (Pro Developer)

 

Accessing Content from a WebBrowser Control

The previous example showed that you could do a lot with the WebBrowser control. However, it did not allow you to access the contents of the documents that are loaded. To give a simple demonstration of what is possible, I created a new form in the WindowsWebAccess project named DocumentTest. I added two Panel controls. I docked one to the bottom, and I docked the other using DockStyle.Fill as the value for the Dock property. I sized them so that the bottom panel takes up a very small slice at the bottom of the screen. I added a WebBrowser control to the top panel and a button to the bottom panel named btnSubmit. In Design view, the form looked like Figure 8-5.

Figure 8-5: The DocumentTest form in Design view

I added some code to the Activated event of the form and the Click event handler on the button control, as shown in Listing 8-2.

Listing 8-2: Code for the DocumentTest Form

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsWebAccess { public partial class DocumentTest : Form { public DocumentTest() { InitializeComponent(); } private void DocumentTest_Activated(object sender, EventArgs e) { string theDoc = "<html><body><form id='myForm'>" + "Enter Name: <input id='name' /></form></body></html>"; this.webBrowser1.DocumentText = theDoc; } private void btnSubmit_Click(object sender, EventArgs e) { System.Windows.Forms.HtmlDocument doc = this.webBrowser1.Document; if (doc != null && doc.All["name"] != null && doc.All["name"].GetAttribute("value").Length == 0) { System.Windows.Forms.MessageBox.Show( "Please enter a name!"); } else { System.Windows.Forms.MessageBox.Show("You entered: " + doc.All["name"].GetAttribute("value")); } } } }

The DocumentTest_Activated event handler sets the DocumentText property of the WebBrowser control. The text is a very simple HTML page that contains a form with some literal text and an input control. The input control is named name.

The btnSubmit_Click event handler gets the Document property of the WebBrowser control, which it uses to determine whether anything was entered in the name input control. If nothing was entered, the screen displays the message box shown in Figure 8-6.

Figure 8-6: DocumentTest form showing the message box that appears when no name is entered

If you enter something in the input control, the message shown in Figure 8-7 appears.

Figure 8-7: DocumentTest form showing the message box that appears when a name is entered

The All property of the HtmlDocument class is a collection of all HtmlElement objects for the document. You can also access the elements of a single form, by using the Forms property of the HtmlDocument object, which is a collection of all the forms in the document.

The DocumentTest example is a simple one; however, the ability to access the properties of a Web Form, as well as the actual HTML, allows you to control the Web Form in new and different ways.

 

Категории