简体   繁体   中英

On change event on WebBrowser in c#

Is there any "reload" event for WebBrowser?

I mean something event which will print some debug message to the console when the page itself reload.

You can listen the DocumentCompleted Event, and print to console. This event is fired every time it finishes downloading a document, so it could fire more than once for 1 URL. Just make sure you check the Url property of DocumentCompleted and compare it to your URL.

To print a message for debug:

System.Diagnostics.Debug.WriteLine("Send to debug output.");

Edit: More info: https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted(v=vs.110).aspx

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["NextLoad"] == true)
        {
            //save to log
        }else 
        {
            Session["NextLoad"] = true;
        }

    }

You could also do based on postback..

protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            //save to log
        }
    }

Or, if you need to persist beyond session and don't want to use cookies, stick the user's ip into the database and check to see if it exists on next visit. If it does, write the log.

protected void Page_Load(object sender, EventArgs e)
    {
       if(!does_User_IP_Exist_In_DB(user_IP)){
            add_User_IP(user_IP);
       }else{
            write_To_Log();
       }
    }

The use of FRAMES changes things. You will want to use:

// event handler for when a document (or frame) has completed its download
Timer m_pageHasntChangedTimer = null;
private void webBrowser_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e ) {
// dynamic pages will often be loaded in parts e.g. multiple frames
// need to check the page has remained static for a while before safely saying it is 'loaded'
// use a timer to do this

// destroy the old timer if it exists
if ( m_pageHasntChangedTimer != null ) {
    m_pageHasntChangedTimer.Dispose();
}

// create a new timer which calls the 'OnWebpageReallyLoaded' method after 200ms
// if additional frame or content is downloads in the meantime, this timer will be destroyed
// and the process repeated
m_pageHasntChangedTimer = new Timer();
EventHandler checker = delegate( object o1, EventArgs e1 ) {
    // only if the page has been stable for 200ms already
    // check the official browser state flag, (euphemistically called) 'Ready'
    // and call our 'OnWebpageReallyLoaded' method
    if ( WebBrowserReadyState.Complete == webBrowser.ReadyState ) {
        m_pageHasntChangedTimer.Dispose();
        OnWebpageReallyLoaded();
    }
};
m_pageHasntChangedTimer.Tick += checker;
m_pageHasntChangedTimer.Interval = 200;
m_pageHasntChangedTimer.Start();
}

OnWebpageReallyLoaded() {
/* place your harvester code here */
}

Taken from: HTML - How do I know when all frames are loaded?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM