WebBrowser control and not updating contents

J

Jimski

Hi,

I am using a WebBrowser (.NET 2.0) control on my form and want to
dynamically build the HTML for it.

However, I am unable to get the Browser to show the HTML I am writing
to the browser.

I am using the following code in a button click event on the form.

Why does the code below not work? At the time of
Application.DoEvents() the body contains HTML which I have
subsequently copied and displayed in another browser without any
issues?

Adding is just the first step, I would like to be able to add <DIV>
tags too and dynamically change the contents of those <DIV> tags at
runtime. So far I am not getting very far!

Help appreciated asap please!!

Regards
Jimski

htmlDisplay.Navigate("about:blank");

//Set document
HtmlDocument doc = htmlDisplay.Document.D;
while (doc.Body == null)
{
Application.DoEvents();
}
HtmlElement elementFrameworkTbl =
doc.CreateElement("TABLE");
doc.Body.AppendChild(elementFrameworkTbl);
elementFrameworkTbl.SetAttribute("border", "5");
elementFrameworkTbl.SetAttribute("height", "100%");
elementFrameworkTbl.SetAttribute("width", "100%");

HtmlElement tableRow = doc.CreateElement("TR");
elementFrameworkTbl.AppendChild(tableRow);

HtmlElement tableCell = doc.CreateElement("TD");
tableRow.AppendChild(tableCell);
tableCell.InnerText = "tableText";

Application.DoEvents();
 
W

Wil Peck

Have you tried calling the Refresh method on the WebBrowser control? I
would think that even though you've changed the contents of the web browser
control programmatically, nothing has told the control to reload itself with
the new contents you created. Reading the documentation on the Refresh
method eludes to the fact that the refresh method really is for requesting
an updated document from the server. In this case, the code in your form is
acting like the server and your browser needs to refresh. I don't think
Application.DoEvents will cause your browser to refresh - but I could be
wrong.

Hope this helps,
Wil
 
J

Jimski

Hi Wil,

Thanks for coming back to me, I have tried Refresh and it does not
work as expected. I also read it like you too, i.e. that it refreshes
the page being passed in.

Any other ideas would be most helpful!

Cheers
Jimski
 
W

Wil Peck

Ok - I wasn't able to update the Document object directly for a couple
reasons.

1. The Document property is null if you access it without initializing the
DocumentText with a valid set of html text.
2. After initializing the DocumentText property the Body Property was always
null no matter what combinations of text was placed into the DocumentText
property. This obviously included the <body></body> tags.

So, as a hack just to get what you were looking displayed in the browser
control I did as follows.

StringWriter writer = new StringWriter();

HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);

htmlWriter.RenderBeginTag("html");
htmlWriter.RenderBeginTag("body");
htmlWriter.RenderBeginTag("table");

htmlWriter.AddAttribute(HtmlTextWriterAttribute.Border,
"5");
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Height,
"100%");
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Width,
"100%");

htmlWriter.RenderBeginTag("tr");
htmlWriter.RenderBeginTag("td");
htmlWriter.Write("test text");
htmlWriter.RenderEndTag(); //td
htmlWriter.RenderEndTag(); //tr
htmlWriter.RenderEndTag(); //table
htmlWriter.RenderEndTag(); //body
htmlWriter.RenderEndTag(); //html
htmlWriter.Flush();

webBrowser1.DocumentStream = new
MemoryStream(Encoding.UTF8.GetBytes(writer.ToString()));

Hopefully this will at least get you going until you can figure out how to
properly create this html layout using the Web Browser control.

Another thing you might consider is using a combination Xml and Xslt to
create your dynamic HTML output depending on how complex your output is.
It's pretty easy go get going using the .NET System.Xml.Xsl and System.Xml
namespaces. Let me know if you need further references for those.

HTH - Wil
 
J

Jimski

Hi Wil,

Once again thanks for coming back to me. I agree your idea is a good
one, it works.....wait for it...but....I need to keep a reference to
the htmlElements in order that I can update areas of the web browser
control (i.e. contents of a <DIV>)...

Therefore by replacing the whole document I am not going to be able to
do that.

Might be time to give up and find an alternative method...or a 3rd
party control.

Cheers
Jimski
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top