.NET Framework Developer Center >
.NET Development Forums
>
.NET Base Class Library
>
WebBrowserComplete only firing once.
WebBrowserComplete only firing once.
- This is my event:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//stuff
//screenscraping,datatable creation
}
This is a button on my form:
private void button2_Click(object sender, EventArgs e)
{
//Navigate to the URL in the row dictated by the counter value, get val of 3rd column over
webBrowser1.Navigate(dataGridView1[2,citycounter].Value.ToString());
citycounter++;
}
------------------------------------------
When I click the button, my webbrowser object navigates to the correct page, and the documentcomplete event fires, causing "stuff" to happen. When I click the button again, webbrowser object navigates to the correct page, but does not fire the documentcomplete event. Everytime I click, the browser reacts correctly and navigates to the next page in my datagrid, but the documentcomplete event does not fire after the first time.
Any ideas?
Answers
- You just might be seeing cached pages.
Try out this browser demo.
I get similar results when I re-visit the page over and over.
class ProgressBrowser : Form
{
#region form.designer.cs file
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btn_Go = new System.Windows.Forms.Button();
this.btn_Refresh = new System.Windows.Forms.Button();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.txt_StatusBox = new System.Windows.Forms.TextBox();
this.lbl_StatusTxt = new System.Windows.Forms.Label();
this.cbo_AddressBar = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btn_Go
//
this.btn_Go.Location = new System.Drawing.Point(466, 11);
this.btn_Go.Name = "btn_Go";
this.btn_Go.Size = new System.Drawing.Size(75, 23);
this.btn_Go.TabIndex = 1;
this.btn_Go.Text = "GO";
this.btn_Go.UseVisualStyleBackColor = true;
this.btn_Go.Click += new System.EventHandler(this.btn_Go_Click);
//
// btn_Refresh
//
this.btn_Refresh.Location = new System.Drawing.Point(548, 10);
this.btn_Refresh.Name = "btn_Refresh";
this.btn_Refresh.Size = new System.Drawing.Size(75, 23);
this.btn_Refresh.TabIndex = 2;
this.btn_Refresh.Text = "Refresh";
this.btn_Refresh.UseVisualStyleBackColor = true;
this.btn_Refresh.Click += new System.EventHandler(this.btn_Refresh_Click);
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(12, 40);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(611, 476);
this.webBrowser1.TabIndex = 3;
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(119, 523);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(371, 23);
this.progressBar1.TabIndex = 4;
//
// txt_StatusBox
//
this.txt_StatusBox.Location = new System.Drawing.Point(13, 523);
this.txt_StatusBox.Name = "txt_StatusBox";
this.txt_StatusBox.Size = new System.Drawing.Size(100, 20);
this.txt_StatusBox.TabIndex = 5;
//
// lbl_StatusTxt
//
this.lbl_StatusTxt.AutoSize = true;
this.lbl_StatusTxt.Location = new System.Drawing.Point(13, 550);
this.lbl_StatusTxt.Name = "lbl_StatusTxt";
this.lbl_StatusTxt.Size = new System.Drawing.Size(68, 13);
this.lbl_StatusTxt.TabIndex = 6;
this.lbl_StatusTxt.Text = "lbl_StatusTxt";
//
// cbo_AddressBar
//
this.cbo_AddressBar.FormattingEnabled = true;
this.cbo_AddressBar.Location = new System.Drawing.Point(12, 13);
this.cbo_AddressBar.Name = "cbo_AddressBar";
this.cbo_AddressBar.Size = new System.Drawing.Size(448, 21);
this.cbo_AddressBar.TabIndex = 7;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(497, 532);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 8;
this.label2.Text = "totalProgress";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(497, 554);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 9;
this.label1.Text = "currentProgress";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(633, 576);
this.Controls.Add(this.label1);
this.Controls.Add(this.label2);
this.Controls.Add(this.cbo_AddressBar);
this.Controls.Add(this.lbl_StatusTxt);
this.Controls.Add(this.txt_StatusBox);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.btn_Refresh);
this.Controls.Add(this.btn_Go);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btn_Go;
private System.Windows.Forms.Button btn_Refresh;
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.TextBox txt_StatusBox;
private System.Windows.Forms.Label lbl_StatusTxt;
private System.Windows.Forms.ComboBox cbo_AddressBar;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
#endregion
#region form.cs file
public ProgressBrowser()
{
InitializeComponent();
Initialize_this();
this.Load += new EventHandler(Form1_Load);
}
#region Initialize Methods
private void Initialize_this() // Template Method Pattern
{
this.Initialize_WebBrowser();
this.Initialize_ProgressBar();
this.Initialize_AddressBar();
return;
}
private void Initialize_WebBrowser()
{
this.webBrowser1.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
this.webBrowser1.ProgressChanged +=
new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged);
this.webBrowser1.Navigated +=
new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
this.webBrowser1.Navigating +=
new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
this.webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);
return;
}
private void Initialize_ProgressBar()
{
this.progressBar1.Style = ProgressBarStyle.Continuous;
this.progressBar1.Step = 10;
return;
}
private void Initialize_AddressBar()
{
this.cbo_AddressBar.SelectedIndexChanged +=
new EventHandler(cbo_AddressBar_SelectedIndexChanged);
}
#endregion
#region Private Methods
private void Navigate(Uri uri)
{
this.webBrowser1.Url = uri;
URL_Add(uri);
this.cbo_AddressBar.Text = uri.ToString();
this.progressBar1.PerformStep();
}
private void URL_Add(Uri uri)
{
if (!this.cbo_AddressBar.Items.Contains(uri))
{
this.cbo_AddressBar.Items.Add(uri);
}
}
#endregion
#region Event Handlers
void Form1_Load(
object sender,
EventArgs e)
{
this.Navigate(new Uri(HomePage));
return;
}
private void btn_Go_Click(
object sender,
EventArgs e)
{
this.Navigate(new Uri(this.cbo_AddressBar.Text));
}
private void btn_Refresh_Click(
object sender,
EventArgs e)
{
this.Navigate(new Uri(this.cbo_AddressBar.Text));
}
private void webBrowser1_DocumentCompleted(
object sender,
WebBrowserDocumentCompletedEventArgs e)
{
switch (this.webBrowser1.ReadyState)
{
case WebBrowserReadyState.Complete:
//this.progressBar1.Value = this.progressBar1.Maximum;
this.progressBar1.Visible = false;
this.label1.Text = "totalProgress";
this.label2.Text = "currentProgress";
break;
case WebBrowserReadyState.Interactive:
//this.progressBar1.PerformStep();
break;
case WebBrowserReadyState.Loaded:
//this.progressBar1.PerformStep();
break;
case WebBrowserReadyState.Loading:
//this.progressBar1.PerformStep();
break;
case WebBrowserReadyState.Uninitialized:
//this.progressBar1.Value = this.progressBar1.Minimum;
this.progressBar1.Visible = false;
break;
default:
break;
}
return;
}
private void webBrowser1_ProgressChanged(
object sender,
WebBrowserProgressChangedEventArgs e)
{
long progress = e.CurrentProgress;
//
if (e.CurrentProgress > 0)
{
this.currentProgress = (int)progress;
this.label2.Text = e.CurrentProgress.ToString();
this.label1.Text = e.MaximumProgress.ToString();
}
this.txt_StatusBox.Text = this.webBrowser1.ReadyState.ToString();
if (this.webBrowser1.IsBusy)
{
try
{
this.progressBar1.Maximum = (int)e.MaximumProgress;
}
catch (Exception ex)
{
string msg = ex.Message;
this.progressBar1.Maximum = Int32.MaxValue;
}
if (e.CurrentProgress <= e.MaximumProgress)
{
this.progressBar1.Value = (int)progress;
}
}
else
{
this.progressBar1.Value = this.progressBar1.Minimum;
}
return;
}
private void webBrowser1_Navigated(
object sender,
WebBrowserNavigatedEventArgs e)
{
this.cbo_AddressBar.Text = this.webBrowser1.Url.ToString();
this.URL_Add(this.webBrowser1.Url);
return;
}
private void webBrowser1_Navigating(
object sender,
WebBrowserNavigatingEventArgs e)
{
this.progressBar1.Visible = true;
return;
}
private void webBrowser1_StatusTextChanged(
object sender,
EventArgs e)
{
this.lbl_StatusTxt.Text = this.webBrowser1.StatusText;
return;
}
private void cbo_AddressBar_SelectedIndexChanged(
object sender,
EventArgs e)
{
Uri selectedURL = (Uri)this.cbo_AddressBar.SelectedItem;
this.Navigate(selectedURL);
return;
}
#endregion
private string HomePage = @"http://www.microsoft.com/";
private int currentProgress = 1;
#endregion
}
Mark the best replies as answers. "Fooling computers since 1971."- Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 7:42 AM
All Replies
- Hey Derek,Can you try isolating the problem?Start a new project, drop on a button (dock it top) and web browser. Add a couple event handlers:private void button1_Click(object sender, EventArgs e){webBrowser1.Navigate("yahoo.ca");}private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e){Debug.WriteLine(string.Format("woot {0}", DateTime.Now.ToLongTimeString()));}I noticed in running this that Document completed comes back once, real quick, then a couple times a second or two later.If nothing else comes into play, I might suspect that you have some long-running thing happening in the browser (or on the server)?Can you debug the server to see what's going on? If you run a debug session locally, can you just step through and see if there's something happening otherwise?Cheers,-jc
Me, coding and stuff: Mr. James - Well, the page I'm hitting is http://www.mta.info/nyct/index.html.
I'm scraping the news section for links, and then visiting each link sequentially.
The first link opens correctly, and executes //stuff (populates a datagrid, and ticks a counter.)
second link opens correctly, but does not execute //stuff
This is regardless of the page I point the browser at.
If I force page visit counter to start from URL 1 instead of 0, then URL 1 will work properly
and URL 2 will fail to trigger DocumentComplete.
If I visit URL 1, then URL 0, then URL 1 will work, and URL 0 will fail to trigger DocumentComplete.
I'm new to this whole coding game, but I think I've ruled out it being a server side issue right? - You just might be seeing cached pages.
Try out this browser demo.
I get similar results when I re-visit the page over and over.
class ProgressBrowser : Form
{
#region form.designer.cs file
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btn_Go = new System.Windows.Forms.Button();
this.btn_Refresh = new System.Windows.Forms.Button();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.txt_StatusBox = new System.Windows.Forms.TextBox();
this.lbl_StatusTxt = new System.Windows.Forms.Label();
this.cbo_AddressBar = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btn_Go
//
this.btn_Go.Location = new System.Drawing.Point(466, 11);
this.btn_Go.Name = "btn_Go";
this.btn_Go.Size = new System.Drawing.Size(75, 23);
this.btn_Go.TabIndex = 1;
this.btn_Go.Text = "GO";
this.btn_Go.UseVisualStyleBackColor = true;
this.btn_Go.Click += new System.EventHandler(this.btn_Go_Click);
//
// btn_Refresh
//
this.btn_Refresh.Location = new System.Drawing.Point(548, 10);
this.btn_Refresh.Name = "btn_Refresh";
this.btn_Refresh.Size = new System.Drawing.Size(75, 23);
this.btn_Refresh.TabIndex = 2;
this.btn_Refresh.Text = "Refresh";
this.btn_Refresh.UseVisualStyleBackColor = true;
this.btn_Refresh.Click += new System.EventHandler(this.btn_Refresh_Click);
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(12, 40);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(611, 476);
this.webBrowser1.TabIndex = 3;
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(119, 523);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(371, 23);
this.progressBar1.TabIndex = 4;
//
// txt_StatusBox
//
this.txt_StatusBox.Location = new System.Drawing.Point(13, 523);
this.txt_StatusBox.Name = "txt_StatusBox";
this.txt_StatusBox.Size = new System.Drawing.Size(100, 20);
this.txt_StatusBox.TabIndex = 5;
//
// lbl_StatusTxt
//
this.lbl_StatusTxt.AutoSize = true;
this.lbl_StatusTxt.Location = new System.Drawing.Point(13, 550);
this.lbl_StatusTxt.Name = "lbl_StatusTxt";
this.lbl_StatusTxt.Size = new System.Drawing.Size(68, 13);
this.lbl_StatusTxt.TabIndex = 6;
this.lbl_StatusTxt.Text = "lbl_StatusTxt";
//
// cbo_AddressBar
//
this.cbo_AddressBar.FormattingEnabled = true;
this.cbo_AddressBar.Location = new System.Drawing.Point(12, 13);
this.cbo_AddressBar.Name = "cbo_AddressBar";
this.cbo_AddressBar.Size = new System.Drawing.Size(448, 21);
this.cbo_AddressBar.TabIndex = 7;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(497, 532);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 8;
this.label2.Text = "totalProgress";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(497, 554);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 9;
this.label1.Text = "currentProgress";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(633, 576);
this.Controls.Add(this.label1);
this.Controls.Add(this.label2);
this.Controls.Add(this.cbo_AddressBar);
this.Controls.Add(this.lbl_StatusTxt);
this.Controls.Add(this.txt_StatusBox);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.btn_Refresh);
this.Controls.Add(this.btn_Go);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btn_Go;
private System.Windows.Forms.Button btn_Refresh;
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.TextBox txt_StatusBox;
private System.Windows.Forms.Label lbl_StatusTxt;
private System.Windows.Forms.ComboBox cbo_AddressBar;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
#endregion
#region form.cs file
public ProgressBrowser()
{
InitializeComponent();
Initialize_this();
this.Load += new EventHandler(Form1_Load);
}
#region Initialize Methods
private void Initialize_this() // Template Method Pattern
{
this.Initialize_WebBrowser();
this.Initialize_ProgressBar();
this.Initialize_AddressBar();
return;
}
private void Initialize_WebBrowser()
{
this.webBrowser1.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
this.webBrowser1.ProgressChanged +=
new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged);
this.webBrowser1.Navigated +=
new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
this.webBrowser1.Navigating +=
new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
this.webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);
return;
}
private void Initialize_ProgressBar()
{
this.progressBar1.Style = ProgressBarStyle.Continuous;
this.progressBar1.Step = 10;
return;
}
private void Initialize_AddressBar()
{
this.cbo_AddressBar.SelectedIndexChanged +=
new EventHandler(cbo_AddressBar_SelectedIndexChanged);
}
#endregion
#region Private Methods
private void Navigate(Uri uri)
{
this.webBrowser1.Url = uri;
URL_Add(uri);
this.cbo_AddressBar.Text = uri.ToString();
this.progressBar1.PerformStep();
}
private void URL_Add(Uri uri)
{
if (!this.cbo_AddressBar.Items.Contains(uri))
{
this.cbo_AddressBar.Items.Add(uri);
}
}
#endregion
#region Event Handlers
void Form1_Load(
object sender,
EventArgs e)
{
this.Navigate(new Uri(HomePage));
return;
}
private void btn_Go_Click(
object sender,
EventArgs e)
{
this.Navigate(new Uri(this.cbo_AddressBar.Text));
}
private void btn_Refresh_Click(
object sender,
EventArgs e)
{
this.Navigate(new Uri(this.cbo_AddressBar.Text));
}
private void webBrowser1_DocumentCompleted(
object sender,
WebBrowserDocumentCompletedEventArgs e)
{
switch (this.webBrowser1.ReadyState)
{
case WebBrowserReadyState.Complete:
//this.progressBar1.Value = this.progressBar1.Maximum;
this.progressBar1.Visible = false;
this.label1.Text = "totalProgress";
this.label2.Text = "currentProgress";
break;
case WebBrowserReadyState.Interactive:
//this.progressBar1.PerformStep();
break;
case WebBrowserReadyState.Loaded:
//this.progressBar1.PerformStep();
break;
case WebBrowserReadyState.Loading:
//this.progressBar1.PerformStep();
break;
case WebBrowserReadyState.Uninitialized:
//this.progressBar1.Value = this.progressBar1.Minimum;
this.progressBar1.Visible = false;
break;
default:
break;
}
return;
}
private void webBrowser1_ProgressChanged(
object sender,
WebBrowserProgressChangedEventArgs e)
{
long progress = e.CurrentProgress;
//
if (e.CurrentProgress > 0)
{
this.currentProgress = (int)progress;
this.label2.Text = e.CurrentProgress.ToString();
this.label1.Text = e.MaximumProgress.ToString();
}
this.txt_StatusBox.Text = this.webBrowser1.ReadyState.ToString();
if (this.webBrowser1.IsBusy)
{
try
{
this.progressBar1.Maximum = (int)e.MaximumProgress;
}
catch (Exception ex)
{
string msg = ex.Message;
this.progressBar1.Maximum = Int32.MaxValue;
}
if (e.CurrentProgress <= e.MaximumProgress)
{
this.progressBar1.Value = (int)progress;
}
}
else
{
this.progressBar1.Value = this.progressBar1.Minimum;
}
return;
}
private void webBrowser1_Navigated(
object sender,
WebBrowserNavigatedEventArgs e)
{
this.cbo_AddressBar.Text = this.webBrowser1.Url.ToString();
this.URL_Add(this.webBrowser1.Url);
return;
}
private void webBrowser1_Navigating(
object sender,
WebBrowserNavigatingEventArgs e)
{
this.progressBar1.Visible = true;
return;
}
private void webBrowser1_StatusTextChanged(
object sender,
EventArgs e)
{
this.lbl_StatusTxt.Text = this.webBrowser1.StatusText;
return;
}
private void cbo_AddressBar_SelectedIndexChanged(
object sender,
EventArgs e)
{
Uri selectedURL = (Uri)this.cbo_AddressBar.SelectedItem;
this.Navigate(selectedURL);
return;
}
#endregion
private string HomePage = @"http://www.microsoft.com/";
private int currentProgress = 1;
#endregion
}
Mark the best replies as answers. "Fooling computers since 1971."- Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 7:42 AM


