I am using the following code to use a webBrowser control in an aspx website as a class-
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;
}
}
This works fine however I wish to add other methods to the class that use the same WebBrowser
object so adding something like -
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");
}
However I can't access the WebBrowser
object as it is not global and also if I set it as global aspx does not allow this is this possible?