inserting text into WebBrowser control
-
Hi everyone! I need to insert text into a WebBrowser control dynamically. I'm thinking it may be possible using Javascript but my problem now is that I don't know how this is done using C#. Any tutorials please? Thanks.
There are at least two ways to get some text displayed by a WebBrowser Control: 1. create an HTML page with whatever content you like, then have the WebControl
Navigate
to it. This requires an HTTP server, which you could embed in your application itself, see e.g. here: Sample HTTP Server Skeleton in C#[^] 2. Much easier is to feed the HTML document straight to theWebBrowser.DocumentText
property. Neither of these let you just append text to existing text, as HTML code isn't "appendable"; it requires correct closing BODY and HTML tags at the end. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
There are at least two ways to get some text displayed by a WebBrowser Control: 1. create an HTML page with whatever content you like, then have the WebControl
Navigate
to it. This requires an HTTP server, which you could embed in your application itself, see e.g. here: Sample HTTP Server Skeleton in C#[^] 2. Much easier is to feed the HTML document straight to theWebBrowser.DocumentText
property. Neither of these let you just append text to existing text, as HTML code isn't "appendable"; it requires correct closing BODY and HTML tags at the end. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
Luc Pattyn wrote:
This requires an HTTP server
No it doesn't! All you need to do is to save it to disk and navigate to a file system url:File URIs in Windows[^]. Sample:
file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc
Cheers!
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
-
Hi everyone! I need to insert text into a WebBrowser control dynamically. I'm thinking it may be possible using Javascript but my problem now is that I don't know how this is done using C#. Any tutorials please? Thanks.
-
Luc Pattyn wrote:
This requires an HTTP server
No it doesn't! All you need to do is to save it to disk and navigate to a file system url:File URIs in Windows[^]. Sample:
file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc
Cheers!
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
You're right of course, however I'm not going to suggest to cause continuous disk activity just to show a growing page of text in some Control. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hi everyone! I need to insert text into a WebBrowser control dynamically. I'm thinking it may be possible using Javascript but my problem now is that I don't know how this is done using C#. Any tutorials please? Thanks.
Take a look at the Document.InvokeScript method of your WebBrowser control. You can execute all the Java Script you want dynamically. Mix that with the ObjectForScripting method and you can handle call backs also. I encapsulated google maps into Windows Forms using this method and it has been flawless.
-
Take a look at the Document.InvokeScript method of your WebBrowser control. You can execute all the Java Script you want dynamically. Mix that with the ObjectForScripting method and you can handle call backs also. I encapsulated google maps into Windows Forms using this method and it has been flawless.
Thanks all. I will try all suggested solutions and see if they will suit my work. However, what may be another problem considering the suggestions is that the application is not aware of the text that will be inserted into the page. All text will be generated as the user is using and interacting with the application. I don't know how I can accomplish that.
-
Thanks all. I will try all suggested solutions and see if they will suit my work. However, what may be another problem considering the suggestions is that the application is not aware of the text that will be inserted into the page. All text will be generated as the user is using and interacting with the application. I don't know how I can accomplish that.
Ok, I will go a step further. There is nothing in the DOM that you cannot alter dynamically with InvokeScript. If you can do it in HTML, you can do it with C#. Here is an example of changing a
element.
public void UpdateHeader(string data)
{
string cmd = "var x=document.getElementById(\"myHeader\"); x.innerHTML = \"{VAR}\";"
cmd = cmd.Replace("{VAR}", data);
object[] codeString = {cmd};
webBrowser1.Document.InvokeScript("eval", codeString);
} -
Ok, I will go a step further. There is nothing in the DOM that you cannot alter dynamically with InvokeScript. If you can do it in HTML, you can do it with C#. Here is an example of changing a
element.
public void UpdateHeader(string data)
{
string cmd = "var x=document.getElementById(\"myHeader\"); x.innerHTML = \"{VAR}\";"
cmd = cmd.Replace("{VAR}", data);
object[] codeString = {cmd};
webBrowser1.Document.InvokeScript("eval", codeString);
} -
Ok, I will go a step further. There is nothing in the DOM that you cannot alter dynamically with InvokeScript. If you can do it in HTML, you can do it with C#. Here is an example of changing a
element.
public void UpdateHeader(string data)
{
string cmd = "var x=document.getElementById(\"myHeader\"); x.innerHTML = \"{VAR}\";"
cmd = cmd.Replace("{VAR}", data);
object[] codeString = {cmd};
webBrowser1.Document.InvokeScript("eval", codeString);
} -
Thanks Potter. In this case, should "eval" be a script that should exist on disk in the application folder? I checked the arguments to InvokeScript and the first argument is name of the script.
"eval" is used when you want to execute Java Script. It is already available in browsers that support Java Script. The commands that you build will be passed to the eval function as a parameter and executed. eval(script);