Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. inserting text into WebBrowser control

inserting text into WebBrowser control

Scheduled Pinned Locked Moved C#
csharpjavascripthelpquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Danzy83
    wrote on last edited by
    #1

    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.

    L L M 3 Replies Last reply
    0
    • D Danzy83

      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.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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 the WebBrowser.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

      M 1 Reply Last reply
      0
      • L Luc Pattyn

        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 the WebBrowser.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

        M Offline
        M Offline
        Manfred Rudolf Bihy
        wrote on last edited by
        #3

        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:

        Quote: IEBlog[^]

        file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc

        Cheers!

        "With sufficient thrust, pigs fly just fine."

        Ross Callon, The Twelve Networking Truths, RFC1925

        L 1 Reply Last reply
        0
        • D Danzy83

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You could use an AJAX-call using JavaScript. You'd still need to embed everything in a HTML-document, but that would be the way to get dynamic content on a page. Start here[^].

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          • M Manfred Rudolf Bihy

            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:

            Quote: IEBlog[^]

            file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc

            Cheers!

            "With sufficient thrust, pigs fly just fine."

            Ross Callon, The Twelve Networking Truths, RFC1925

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • D Danzy83

              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.

              M Offline
              M Offline
              Michael Potter
              wrote on last edited by
              #6

              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.

              D 1 Reply Last reply
              0
              • M Michael Potter

                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.

                D Offline
                D Offline
                Danzy83
                wrote on last edited by
                #7

                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.

                M 1 Reply Last reply
                0
                • D Danzy83

                  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.

                  M Offline
                  M Offline
                  Michael Potter
                  wrote on last edited by
                  #8

                  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);
                  }

                  D 2 Replies Last reply
                  0
                  • M Michael Potter

                    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);
                    }

                    D Offline
                    D Offline
                    Danzy83
                    wrote on last edited by
                    #9

                    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.

                    M 1 Reply Last reply
                    0
                    • M Michael Potter

                      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);
                      }

                      D Offline
                      D Offline
                      Danzy83
                      wrote on last edited by
                      #10

                      I'm also getting a null reference exception. WebBrowser.Document is has null value. I can't also create a new document because WebBrowser.Document has no constructor. I'm stuck.

                      1 Reply Last reply
                      0
                      • D Danzy83

                        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.

                        M Offline
                        M Offline
                        Michael Potter
                        wrote on last edited by
                        #11

                        "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);

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups