原文
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.webbrowser.document(VS.80).aspx
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText =
"<html><body>Please enter your name:<br/>" +
"<input type='text' name='userName'/><br/>" +
"<a href='http://www.microsoft.com'>continue</a>" +
"</body></html>";
webBrowser1.Navigating +=
new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
//如果把上面这句赋值注释掉的话, webBrowser1控件就不会显示DocumentText被赋值的内容,而是一片空白,
//我很疑惑, 为什么不处理Navigating事件, webBrowser1控件就不显示内容,而且必须在Navigating中将事件取消,
//如下面代码中的e.Cancel = true;
}
private void webBrowser1_Navigating(object sender,
WebBrowserNavigatingEventArgs e)
{
System.Windows.Forms.HtmlDocument document =
this.webBrowser1.Document;
if (document != null && document.All["userName"] != null &&
String.IsNullOrEmpty(
document.All["userName"].GetAttribute("value")))
{
e.Cancel = true;
System.Windows.Forms.MessageBox.Show(
"You must enter your name before you can navigate to " +
e.Url.ToString());
}
}