2012-03-18 64 views
1

假设我有以下HTML:如何通过C#将文本插入文本区域与Forms.WebBrowser

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

如何将文本插入到C#Htmldocument textarea的?

setattribute()方法不工作,因为没有value字段textarea

那么如何通过C#插入texttextarea

任何意见是非常感谢!
在此先感谢。

回答

0

首先,为您的项目添加对Microsoft.mshtml的引用,或者如果它不在列表中,则文件为C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll。然后,C#代码看起来是这样的:

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; 
    } 
} 
+0

谢谢埃里克,但你可以指定C#和代码,为我增加了从Microsoft.mshtml.tlb refrence不是微软\ Windows \ System32' 下。。 '.. \ Microsoft Visual Studio 10.0 \ Visual Studio Tools for Office \ PIA \ Common文件夹中的mshtml.dll。 – Murthy 2012-03-18 09:31:42

+0

我已根据您的评论更新。 – 2012-03-18 09:44:46

+0

谢谢你的帮助Erik,但是在添加IHTMLDocument2的引用之后,它仍然显示错误“无法找到类型或名称空间名'IHTMLTextAreaElement'(你是否缺少using指令或程序集引用?)”,我需要安装任何SDK的?或者除了HTML文档之外还有其他方法吗? – Murthy 2012-03-18 10:00:29