Welcome Guest Search | Active Topics | Sign In | Register

WebView LoadCompleted event don't fire when WebView NewWindow Options
Minh Giang
Posted: Wednesday, September 24, 2014 8:54:17 AM
Rank: Member
Groups: Member

Joined: 9/11/2014
Posts: 17
Hi eo_support.
I using WebView_NewWindow event in my code:
Code: C#
private void WebView_NewWindow(object sender, NewWindowEventArgs e)
{
      CreateNewTabPage(CreateTabWebView(e.WebView), e.TargetUrl);
      e.Accepted = true;
}
private WebView CreateTabWebView(WebView webview)
{
       webview.BeforeDownload += new BeforeDownloadHandler(WebView_BeforeDownload);
       webview.IsLoadingChanged += new EventHandler(TabWebView_IsLoadingChanged);
       webview.UrlChanged += new EventHandler(WebView_UrlChanged);
       webview.LoadCompleted += new NavigationTaskEventHandler(TabWebView_LoadCompleted);
       webview.NewWindow += new NewWindowHandler(WebView_NewWindow);
       webview.StatusMessageChanged += new EventHandler(WebView_StatusMessageChanged);
       return webview;
}

But webview in new tab don't fire LoadCompleted event.
So I change my code flowing:
Code: C#
private void WebView_NewWindow(object sender, NewWindowEventArgs e)
{
      CreateNewTabPage(CreateTabWebView(new WebView()), e.TargetUrl);  
}
private WebView CreateTabWebView(WebView webview)
{
       webview.BeforeDownload += new BeforeDownloadHandler(WebView_BeforeDownload);
       webview.IsLoadingChanged += new EventHandler(TabWebView_IsLoadingChanged);
       webview.UrlChanged += new EventHandler(WebView_UrlChanged);
       webview.LoadCompleted += new NavigationTaskEventHandler(TabWebView_LoadCompleted);
       webview.NewWindow += new NewWindowHandler(WebView_NewWindow);
       webview.StatusMessageChanged += new EventHandler(WebView_StatusMessageChanged);
       return webview;
}

Remove e.Accepted = true from WebView_NewWindow event, then WebView_NewWindow event fired, And I made my intentions.
But I worry that the new WebView I created different with the webview in e.WebView parameter, so I lost some important properties.
Please show me know my way is correct or not? Sorry for my english is not fluent!
eo_support
Posted: Wednesday, September 24, 2014 9:26:29 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

You must use the existing WebView instead of creating a new WebView when handling NewWindow event. However LoadCompleted event is ONLY fired for Urls that you explicitly loaded with LoadUrl/LoadRequest (setting the WebView's Url calls LoadUrl so that will work too). The reason that you won't get a LoadCompleted event for the initial load for a NewWindow WebView is because the Url loaded inside that WebView is not loaded by your code --- It's loaded by the user. This is the same as user clicking a link inside the WebView. In that case the WebView will navigate to the new link, but it will not fire LoadCompleted event because this is not a load initiated by LoadUrl/LoadRequest.

IsLoadingChanged event does fire in all cases. So you might want to rely on that event instead.

Thanks!
Minh Giang
Posted: Wednesday, September 24, 2014 9:40:19 AM
Rank: Member
Groups: Member

Joined: 9/11/2014
Posts: 17
Hi eo_support!
My way create a new webview, I see it run normal with fully cookies, session...I don't understand what's wrong?
eo_support
Posted: Wednesday, September 24, 2014 11:58:40 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

The difference is, if you use the following code to open a new window:

Code: JavaScript
var new_window = window.open(url, name);


Then new_window would be null if you create a new WebView.

Thanks
Minh Giang
Posted: Wednesday, September 24, 2014 12:05:39 PM
Rank: Member
Groups: Member

Joined: 9/11/2014
Posts: 17
I create new WebView, then assign url
Code: C#
private void WebView_NewWindow(object sender, NewWindowEventArgs e)
        {
            CreateNewTabPage(CreateTabWebView(), e.TargetUrl);
        }

private void CreateNewTabPage(WebView webview, string url)
        {
            TabPage tab = new TabPage();
            mainTabControl.TabPages.Add(tab);
            WebControl wctr = new WebControl();
            wctr.Dock = DockStyle.Fill;
            wctr.WebView = webview;
            tab.Controls.Add(wctr);
            tab.ToolTipText = url;
            mainTabControl.SelectedTab = tab;
            webview.Url = url;
        }

So new webview will open url from e.TargetUrl


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.