简体   繁体   English

如何使用 mshtml 在 textarea 中输入文本

[英]How to enter the text in textarea using mshtml

I am facing a problem I have to fill a TextArea programatically.我面临一个问题,我必须以编程方式填充 TextArea。 I used the following code.我使用了以下代码。

 System.Windows.Forms.HtmlElement reviewText = myDoc.GetElementById("review-text");
 reviewText.InnerText = Program.mainForm.richTextBox1.Text.Trim();

This works fine and it sets the text in TextArea control.这工作正常,它在 TextArea 控件中设置文本。 The problem that I am facing is that this text appears light gary.我面临的问题是这个文本看起来很轻。 When the user clicks over this text it disappears.当用户单击此文本时,它会消失。 It happens only on first click.它仅在第一次单击时发生。

So I tried the following code to first click this box and then set the text.所以我尝试了下面的代码,首先点击这个框,然后设置文本。

 reviewText.InvokeMember("click");

And then tried to set the text in the TextArea but got the same behaviour.然后尝试在 TextArea 中设置文本,但得到了相同的行为。

I dug into the source of the page and found some script associated with this TextArea.我深入研究了页面的源代码,发现了一些与此 TextArea 相关的脚本。

Source of TextArea文本区域的来源

<textarea onkeyup="javascript:yelp.ui.widget.countChars(this.form.comment,5000);"   onkeydown="javascript:yelp.ui.widget.countChars(this.form.comment,5000);" name="comment" id="review-text" class="form400" rows="8" cols="40" style="height: 86px;"></textarea>

Script associated with TextArea与 TextArea 关联的脚本

 <script type="text/javascript">
   var bizName = "Geary Club";
   if ($('review-text')) 
   {
     new yelp.ui.widget.DefaultValueTextField($('review-text'), 'Please write here');
   } 

Will somebody suggest to me how to insert the text into this TextArea control?有人会建议我如何将文本插入此 TextArea 控件吗?

Is this an ASP.NET web app?这是 ASP.NET web 应用程序吗? If it is, the System.Windows.Forms.HtmlElement is a problem.如果是,则 System.Windows.Forms.HtmlElement 有问题。 What I think is going on is that you need to somehow disable the new yelp.ui.widget.DefaultValueTextField JavaScript code when you're pre-filling the TextArea.我认为正在发生的是,当您预填充 TextArea 时,您需要以某种方式禁用新的 yelp.ui.widget.DefaultValueTextField JavaScript 代码。 Are you able to use an ASP.NET TextBox control with the multi-line properties set?您是否可以使用设置了多行属性的 ASP.NET TextBox 控件? If so, I'd use that and change your top code to just set the.Text property of the Textbox control.如果是这样,我会使用它并将您的顶部代码更改为仅设置 Textbox 控件的 .Text 属性。

Either way, in the Javascript (if this is jQuery), I'd do this:无论哪种方式,在 Javascript (如果这是 jQuery)中,我会这样做:

if ($('review-text') && $('review-text').val() != "")
{
  new yelp.ui.widget.DefaultValueTextField($('review-text'), 'Please write here');
}

That way the default value is only set if there is no text.这样,只有在没有文本时才设置默认值。

I changed the HTML of text area.And remove the class="form400 default_valued_text_field" attribute from its html.我更改了文本区域的 HTML。并从其 html 中删除了class="form400 default_valued_text_field"属性。

myDoc = this.webBrowser1.Document;

myDoc.GetElementById("review-text").OuterHtml = "<textarea id=\"review-text\"   onkeyup=\"javascript:ui.widget.countChars(this.form.comment,5000);\" onkeydown=\"javascript:ui.widget.countChars(this.form.comment,5000);\" name=\"comment\" rows=\"8\" cols=\"40\" style=\"height: 86px;\"></textarea>";

After that i set the inner text of this element之后我设置了这个元素的内部文本

myDoc.GetElementById("review-text").InnerText = "Testing";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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