[英]Function for setting default values of controls in web browser control
I wanted to define a public function by which I can set default values of some controls in a web browser control. 我想定义一个公共函数,通过它可以设置Web浏览器控件中某些控件的默认值。 The web browser shows an HTML file. Web浏览器显示一个HTML文件。 Here is what I have come up with so far;however,the "NullReferenceException was unhandled" occurs: 到目前为止,这是我想出的;但是,发生了“未处理NullReferenceException”的情况:
public void SetDefaultValue(string ControlID,string Value)
{
webBrowser1.Document.GetElementById(ControlID).InnerText = Value;
}
You need to wait for the document to get loaded properly 您需要等待文档正确加载
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webBrowser = sender as WebBrowser;
webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
webBrowser1.Document.GetElementById("Name").InnerText = "Hello World";
}
private void btnClick(object sender, EventArgs e)
{
var wbrowser = new WebBrowser(); // or can be existing object
// Add web broswer to form or panel here..
wbrowser.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
wbrowser.Navigate("your_page_url_here");
}
I write an Example For You: Fist Navigate
to your address or set its DocumentText
. 我为您编写了一个示例:拳头Navigate
至您的地址或设置其DocumentText
。
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<html><body><textarea id=\"Name\" name=\"comments\" cols=\"16\" rows=\"1\">Some Things</textarea></body></html>";
}
when your page loaded you can get its element. 页面加载后,您可以获取其元素。
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.GetElementById("Name").InnerText="Some Thing";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.