Windows Develop Bookmark and Share   
 index > Windows Forms General > capturing DOM events in webbrowser control
 

capturing DOM events in webbrowser control

Hi,

I am creating a win app that hosts a web browser control in C#. The web browser renders some local html page. I need to capture the events on that page. I know this can be done with DOM. But how to I handle DOM events? PLZ help.

Thanks,

Hari

HariAdu  Thursday, August 16, 2007 7:41 AM

Hi HariAdu,

We can get all HtmlElement though WebBrower.Document.All. This property returns an instance of HtmlElementCollection, which stores all HtmlElement objects for the document. HtmlElement represents any possible type of element in an HTML document. The class exposes the most common properties you can expect to find on all elements. See my sample below:

Suppose HTML source isas follow:

Code Snippet

<html>
<body>
<script language="jscript">
function showText(strText1, strText2, strText3)
{
document.getElementById("passedText").value = strText1 + strText2 + strText3;
}
</script>
<INPUT id="passedText" type="text" maxLength="25" size="82" >
<input type="button" id="btnClose" value="关闭" onclick="showText('Testing one... ', 'Two... ', 'Three...')" />
</body>
</html>

You form's code may be something like that:

Code Snippet

WebBrowser

public partial class Form6 : Form

public Form6()

private void Form6_Load(object sender, EventArgs e)

this.webBrowser1.Navigate(@"c:\x.htm");

this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

HtmlDocument htmlDoc = this.webBrowser1.Document;

HtmlElement btnElement = htmlDoc.All["btnClose"];

if (btnElement != null)

new HtmlElementEventHandler(btnElement_Click);

void btnElement_Click(object sender, HtmlElementEventArgs e)

MessageBox.Show("Html Button Clicked");

private void button1_Click(object sender, EventArgs e)

HtmlDocument htmlDoc = this.webBrowser1.Document;

HtmlElement btnElement = htmlDoc.All["btnClose"];

if (btnElement != null)

"Click");

Hi HariAdu,

We can get all HtmlElement though WebBrower.Document.All. This property returns an instance of HtmlElementCollection, which stores all HtmlElement objects for the document. HtmlElement represents any possible type of element in an HTML document. The class exposes the most common properties you can expect to find on all elements. See my sample below:

Suppose HTML source isas follow:

Code Snippet

<html>
<body>
<script language="jscript">
function showText(strText1, strText2, strText3)
{
document.getElementById("passedText").value = strText1 + strText2 + strText3;
}
</script>
<INPUT id="passedText" type="text" maxLength="25" size="82" >
<input type="button" id="btnClose" value="关闭" onclick="showText('Testing one... ', 'Two... ', 'Three...')" />
</body>
</html>

You form's code may be something like that:

Code Snippet

WebBrowser

public partial class Form6 : Form

public Form6()

private void Form6_Load(object sender, EventArgs e)

this.webBrowser1.Navigate(@"c:\x.htm");

this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

HtmlDocument htmlDoc = this.webBrowser1.Document;

HtmlElement btnElement = htmlDoc.All["btnClose"];

if (btnElement != null)

new HtmlElementEventHandler(btnElement_Click);

void btnElement_Click(object sender, HtmlElementEventArgs e)

MessageBox.Show("Html Button Clicked");

private void button1_Click(object sender, EventArgs e)

HtmlDocument htmlDoc = this.webBrowser1.Document;

HtmlElement btnElement = htmlDoc.All["btnClose"];

if (btnElement != null)

"Click");


Hi Zhang,

Thanks a lot. It is of great help to me.

-Hari

HariAdu  Wednesday, August 22, 2007 10:58 AM

You can use google to search for other answers

Custom Search

More Threads

• Postback not happening on selectign a treeview node
• MySettings1 debris being generated in designer?
• Messagebox causes hang
• Override HelpProvider F1
• How can I find the file content when downloading the file?
• InfoPath cannot open form in Outlook
• drawing graphics, need to resize client area?
• How do I produce a smooth change in a graph when I hold down a key?
• ToolTips MIA
• Combobox sorting on pocket pc