* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
3.4 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|
| How to: Access the HTML Source in the Managed HTML Document Object Model | 03/30/2017 |
|
|
53db79fa-8a5e-448e-88c2-f54ace3860b6 |
How to: Access the HTML Source in the Managed HTML Document Object Model
The xref:System.Windows.Forms.WebBrowser.DocumentStream%2A and xref:System.Windows.Forms.WebBrowser.DocumentText%2A properties on the xref:System.Windows.Forms.WebBrowser control return the HTML of the current document as it existed when it was first displayed. However, if you modify the page using method and property calls such as xref:System.Windows.Forms.HtmlElement.AppendChild%2A and xref:System.Windows.Forms.HtmlElement.InnerHtml%2A, these changes will not appear when you call xref:System.Windows.Forms.WebBrowser.DocumentStream%2A and xref:System.Windows.Forms.WebBrowser.DocumentText%2A. To obtain the most up-to-date HTML source for the DOM, you must call the xref:System.Windows.Forms.HtmlElement.OuterHtml%2A property on the HTML element.
The following procedure shows how to retrieve the dynamic source and display it in a separate shortcut menu.
Retrieving the dynamic source with the OuterHtml property
-
Create a new Windows Forms application. Start with a single xref:System.Windows.Forms.Form, and call it
Form1. -
Host the xref:System.Windows.Forms.WebBrowser control in your Windows Forms application, and name it
WebBrowser1. For more information, see How to: Add Web Browser Capabilities to a Windows Forms Application. -
Create a second xref:System.Windows.Forms.Form in your application called
CodeForm. -
Add a xref:System.Windows.Forms.RichTextBox control to
CodeFormand set its xref:System.Windows.Forms.Control.Dock%2A property toFill. -
Create a public property on
CodeFormcalledCode.[!code-csharpDisplayWebBrowserCode#1] [!code-vbDisplayWebBrowserCode#1]
-
Add a xref:System.Windows.Forms.Button control named
Button1to your xref:System.Windows.Forms.Form, and monitor for the xref:System.Windows.Forms.Control.Click event. For details on monitoring events, see Events. -
Add the following code to the xref:System.Windows.Forms.Control.Click event handler.
[!code-csharpDisplayWebBrowserCode#2] [!code-vbDisplayWebBrowserCode#2]
Robust Programming
Always test the value of xref:System.Windows.Forms.WebBrowser.Document%2A before attempting to retrieve it. If the current page is not finished loading, xref:System.Windows.Forms.WebBrowser.Document%2A or one or more of its child objects may not be initialized.