HtmlElement nullreferenceexception was unhandled

使用WebBrowser加载页面之后,试图访问其Document,以及其中的HtmlElement, HtmlElements等对象,总是出现异常: HtmlElement nullreferenceexception was unhandled 。

终于在MSDN上找到如下原因:

 

When using the DOM through the WebBrowser control, you should always wait until the DocumentCompleted event occurs before attempting to access the Document property of the WebBrowser control. The DocumentCompleted event is raised after the entire document has loaded; if you use the DOM before then, you risk causing a run-time exception in your application.

 

原来我在Form1_Load函数中,也就是在页面刚加载时就试图去访问这些页面元素,而这时,这些页面还没加载完成,所以得到的总是空。把代码换到DocumentCompleted事件中去,就搞定了。

 

这篇文章的地址是:http://msdn.microsoft.com/en-us/library/ms171712.aspx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值