title: Use webBrowser control in aspx website
body: <p>I am using the following code to use a webBrowser control in an aspx website as a class-</p>
  
  <pre><code>public CustomBrowser()
  {
      //
      // TODO: Add constructor logic here
      //
  }
  
  protected string _url;
  string html = "";
  public string GetWebpage(string url)
  {
      _url = url;
      // WebBrowser is an ActiveX control that must be run in a
      // single-threaded apartment so create a thread to create the
      // control and generate the thumbnail
      Thread thread = new Thread(new ThreadStart(GetWebPageWorker));
      thread.SetApartmentState(ApartmentState.STA);
      thread.Start();
      thread.Join();
      string s = html;
      return s;
  }
  
  protected void GetWebPageWorker()
  {
      using (WebBrowser browser = new WebBrowser())
      {
          //  browser.ClientSize = new Size(_width, _height);
          browser.ScrollBarsEnabled = false;
          browser.ScriptErrorsSuppressed = true;
          browser.Navigate(_url);
          browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
          // Wait for control to load page
          while (browser.ReadyState != WebBrowserReadyState.Complete)
              Application.DoEvents();
  
          html = browser.DocumentText;
  
      }
  }
  </code></pre>
  
  <p>This works fine however I wish to add other methods to the class that use the same <code>WebBrowser</code> object so adding something like -</p>
  
  <pre><code>private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
      {
  
              if (browser.Document.Body.InnerHtml != null)
              {
  
  
                   browse();
  
              }
  
      }
  
  private void browse(object sender, EventArgs e)
      {
  
              browser.Navigate("url");
  
  
      }
  </code></pre>
  
  <p>However I can't access the <code>WebBrowser</code> object as it is not global and also if I set it as global aspx does not allow this is this possible?</p>