简体   繁体   中英

How to insert text into textarea through c# with Forms.WebBrowser

Suppose I had the following HTML:

<html>
<body>
   <textarea id="foo"></textarea>
</body>
</html>

How to insert text into the textarea through C# Htmldocument ?

The setattribute () method is not working as there is no value field for textarea .

So how to insert text into textarea through C#?

Any advice is much appreciated!
Thanks in advance.

First, add a reference to your project for Microsoft.mshtml , or if it is not on the list, the file C:\\Program Files (x86)\\Microsoft.NET\\Primary Interop Assemblies\\Microsoft.mshtml.dll . Then the C# code would look something like:

string myId = "foo";
string myNewValue = "This goes in the Text Area!";

IHTMLDocument2 htmlDoc = webBrowser1.Document.DomDocument as IHtmlDocument2;
if (htmlDoc != null)
{
  var textAreaType = typeof(IHTMLTextAreaElemnt);
  IHTMLTextAreaElement htmlTextArea = 
    htmlDoc.All
           .FirstOrDefault(x => textAreaType.IsAssignableFrom(x)
                                && ((IHTMLElement)x).id == myId) 
           as IHTMLTextAreaElement;
  if (htmlTextArea != null)
  {
    htmlTextArea.value = myNewValue;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM