c# - 如何在 System.Windows.Forms.WebBrowser 中判断加载的页面是错误页面还是有效页面?

标签 c# .net winforms

我正在使用

System.Windows.Forms.WebBrowser

C# 中用于显示某些页面的控件。当用户单击不存在的页面的 url 时,我想做一些自定义工作。

基本上我想在浏览器显示以下消息时设置一些值

The page cannot be displayed 
 The page you are looking for is currently unavailable. The Web site might be experiencing technical difficulties 

如何获取状态以便区分加载页面和错误页面?

最佳答案

如果将 WebBrowser 转换为底层 ActiveX 实现,则可以访问 NavigateError 事件。

注意:您需要添加对 SHDocVw 的引用。令人困惑的是,这是在名称为“Microsoft Internet Controls”的 COM 选项卡中,路径为 c:\windows\system32\ieframe.dll

 private void button1_Click(object sender, EventArgs e)
        {
            //Note: you need to wait until the ActiveXInstance property is initialised.
            var axWebBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
            axWebBrowser.NavigateError += axWebBrowser_NavigateError; 
            webBrowser1.Url = new Uri("http://www.thisisnotavaliddomain.com");
        }

        void axWebBrowser_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
        {
            //handle your error
        }

关于c# - 如何在 System.Windows.Forms.WebBrowser 中判断加载的页面是错误页面还是有效页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987691/

相关文章:

c# - WPF MVVM : Dynamic drawing of a shape from a xaml file

c# - TweetSharp GetRequestToken 此页面的请求 token 无效

c# - 如何增加 WinForms 中 ListViewItems 的 AutoPopDelay 值?

c# - 使用 MVP 模式编写用户界面设置类

c# - Visual Studio C# Win-Forms 应用程序网络浏览器加载速度非常慢

c# - 使用 c# Webclient 通过 https 请求 html

.net - 无法确保我安装了最新版本的 .NET Core

c# - 只有assignment、call、increment、decrement和new对象表达式可以作为语句

C# WPF datgird IvalueConverter 绑定(bind)问题

c# - 搜索特定字符串并返回整行