Windows Develop Bookmark and Share   
 index > Windows Forms General > my if-else statement is running both code blocks? webbrowser control
 

my if-else statement is running both code blocks? webbrowser control

Hello,

I'm using a webbrowser control on a windows forms app. I'm calling the webbrowser control's navigate method in the form's load event to load a particular web page. In the webbrowser's DocumentCompleted event I need to implement some logic which will basically automate a login by using the html DOM. The automated login works fine, I tested that elseware, but its in this DocumentCompleted event that I want to implement it because it will need to occur not only on first page load but after periods of inactivity when the authentication times out I want to also automate the re-logon... in other words, when a session times out and a login is necessary the login page is automatcially loaded, so in this document completed event I will check to see if its the login page and if so, supply the credentials. I want to do this by testing for the existence of the html login controls... so I started with just a little sample code to test/confirm how I could do this check.... and it fires both the true and false code blocks of my 'if' statement?

HtmlElement UsernameElement;

UsernameElement = webBrowserControl.Document.GetElementById("LoginControl_UserName");

if (UsernameElement != null)
{
     MessageBox.Show("username not null");
}
else
{
    MessageBox.Show("username is null");
}

first I see the 'username is null' message, and then it also shows the 'username is not null' message? what the?
c0pe  Wednesday, October 07, 2009 8:17 PM
Think about this.
How could it be possible to see the "is null" message prior to the "not null" message?

DocumentCompleted is firing multiple times.

It is common for DocumentCompleted to fire multiple times a single web page is loaded as different pieces and parts load.
You need to check the WebBrowser.ReadyState property inside of the DocumentCompleted event handler to know if the page is truly finished.

Mark the best replies as answers. "Fooling computers since 1971."
  • Proposed As Answer byReed Copsey, Jr. Wednesday, October 07, 2009 8:32 PM
  • Marked As Answer byc0pe 22 hours 11 minutes ago
  •  
Rudedog2  Wednesday, October 07, 2009 8:29 PM
It might be because documentcompleted event gets fired multiple times, so may be when fired for the first time the UsernameElement is null and the next time it is not null..
so you are getting both messages...

you can use ready state property of the web browser control before excuting your code.. only if WebBrowserReadyState is complete you should execute your code...
let me know if this helps
Paras
  • Marked As Answer byc0pe 22 hours 11 minutes ago
  •  
paras kumar  Wednesday, October 07, 2009 8:34 PM
Think about this.
How could it be possible to see the "is null" message prior to the "not null" message?

DocumentCompleted is firing multiple times.

It is common for DocumentCompleted to fire multiple times a single web page is loaded as different pieces and parts load.
You need to check the WebBrowser.ReadyState property inside of the DocumentCompleted event handler to know if the page is truly finished.

Mark the best replies as answers. "Fooling computers since 1971."
  • Proposed As Answer byReed Copsey, Jr. Wednesday, October 07, 2009 8:32 PM
  • Marked As Answer byc0pe 22 hours 11 minutes ago
  •  
Rudedog2  Wednesday, October 07, 2009 8:29 PM
It might be because documentcompleted event gets fired multiple times, so may be when fired for the first time the UsernameElement is null and the next time it is not null..
so you are getting both messages...

you can use ready state property of the web browser control before excuting your code.. only if WebBrowserReadyState is complete you should execute your code...
let me know if this helps
Paras
  • Marked As Answer byc0pe 22 hours 11 minutes ago
  •  
paras kumar  Wednesday, October 07, 2009 8:34 PM
Don't use MessageBox here.  It pumps a message loop and that allows the WebBrowser to progress.  The DocumentCompleted event will fire again, now producing the 2nd message box.  This kind of problem is why Application.DoEvents() has such a bad reputation, it also causes nested execution of events.  This is normally desirable, you'll want your Paint event to run when a message box is displayed for example so your UI doesn't freeze.  However, putting it in the wrong place can have unexpected side-effects, this is one of them.

Use Console.WriteLine() instead to display diagnostics, they'll show up in the Visual Studio's Output window.  Using the debugger is of course a good solution too.

Hans Passant.
nobugz  Thursday, October 08, 2009 1:10 AM
I assumed it had to be something like that, I know it is impossible for something to be both true and false at the same time... with an event name like 'DocumentCompleted' though, I certainly didn't think it would be firing more than once per page.

thanks for the help!
c0pe  22 hours 8 minutes ago
psst.  it happens to everybody. 

A web page can be made up of multiple sub-documents.  who knew?  not me.

Happy Coding

Rudy   =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  20 hours 49 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Automate PDF reading
• how to close menu programatically in c#
• SplitContainer "Continuous Layout" or "Show Contents During Reizing" Option?
• .sln reader
• Hooking mouse events of a program from my app
• Sorting Strings with numeric values
• MDI Container Event Firing
• Items selection in ListView control
• Best way to capture changes made by PropertyGrid?
• ListViewItemSorter stops working after introducing groups?