Set Value of a TextArea inside a WebBrowser Control (C# / .NET)

孤者浪人 提交于 2019-11-29 08:46:02

Suppose you had the following HTML:

<html>
<body>
   <textarea id='foo'>Testing</textarea>
</body>
</html>

You can set the text in the textarea like this:

HtmlElement textArea = webBrowser1.Document.All["foo"];
if (textArea != null)
{
    textArea.InnerText = "This is a test";
}

A couple of points just in case you haven't realised these:

  • GetElementById will only return a single item or null, it is not a collection.
  • index out of bounds errors will be thrown if you try and insert elements from one instance of the WebBrowser control into elements of another instance of the WebBrowser control.
  • The GetElementBy.. can be run straight from the WebBrowser.Document property so theres no need to access the All[] collection.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!