471,319 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to RemoveChild from HtmlElement from C#


I have a webbrowser control in my winform. There is an html in the
webbrowser which has a table, and many child <tr>s and <td>s inside it.
I want to delete a specific <tr> node completely from the html. (I dont
want to do style = "display:none"). I want to do RemoveChild("id of the
tr") of the table, on some button click.

There is no RemoveChild available on HtmlElement.
MS experts, MVPs....any clues on how to do this?


Feb 6 '06 #1
3 15999
here is the code: (NOTE: you need to add reference to Microsoft.mshtml
assembly first from the .net tab)

Assuming the web browser control name is "browser"

using mshtml;
private void HandleFormLoad (object sender, EventArgs e)
browser.NavigateComplete2 +=new
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Even tHandler

private void NavigationComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Even t e)
HTMLDocumentClass htmlDoc = browser.Document as
IHTMLDOMNode node = htmlDoc.getElementById ("tr2") as
MessageBox.Show ("Click to remove the second row...");
node.parentNode.removeChild (node);

//the testtable.html file looks like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta name="GENERATOR" content="Microsoft Visual Studio .NET
<meta name="ProgId" content="VisualStudio.HTML">
<meta name="Originator" content="Microsoft Visual Studio .NET
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300"
<TR id="tr1">
<TD>Row 1 : Col 1</TD>
<TD>Row 1 : Col 2</TD>
<TD>Row 1 : Col 3</TD>
<TR id="tr2">
<TD>Row 2 : Col&nbsp;1</TD>
<TD>Row 2 : Col 2</TD>
<TD>Row 2 : Col 3</TD>
<TR id="tr3">
<TD>Row 3 : Col 1</TD>
<TD>Row 3 : Col 2</TD>
<TD>Row 3 : Col 3</TD>
Hope this helps...

- NuTcAsE

Feb 6 '06 #2
Hi thanks for that tip.

But, I am using .Net 2.0's WebBrowser control (not AxWebBrowser
HTMLDocumentClass htmlDocument = webBrowser1.Document as


HTMLDocumentClass htmlDocument = (HTMLDocumentClass)

result in.....
Error : Cannot convert type 'System.Windows.Forms.HtmlDocument' to

Any clues?
Thanks in advance,


Feb 6 '06 #3
The WebBrowser.Document is a managed HtmlDocument wrapper, you need to
use the DomDocument property of the HtmlDocument object... code below:

//In the NavigateCompleted event...

HTMLDocumentClass doc = browser.Document.DomDocument as
IHTMLDOMNode node = doc.getElementById ("tr2");
.... //the rest is the same as above.

Hope this helps...
- NuTcAsE

Feb 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jason Keirstead | last post: by
1 post views Thread by Christopher Benson-Manica | last post: by
1 post views Thread by JehanNYNJ | last post: by
1 post views Thread by Rebecca Tsukalas | last post: by
10 posts views Thread by r_ahimsa_m | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.