C#html元素更改输入值 [英] C# html element change input value

查看:114
本文介绍了C#html元素更改输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML代码



this is my html code

<input type="Text" name="MESSAGE" class="pmain" size="30">





所以这样看就像网站上的普通空白文本框一样,我想这样做,当我按下我的程序文本上的按钮就会出现在它上面。



这是我的C#代码





so this would look like a normal blank textbox on a website, I want to make it so when I press a button on my program text will appear on it.

this is my C# code

private void AddNameBtn_Click(object sender, EventArgs e)
     {
         foreach (HtmlElement he in webBrowser1.Document.All.GetElementsByName("MESSAGE"))
         {
                 he.SetAttribute("value", "HI");
         }



     }





这应该是textfeild / html网站上的文本框说HI,但当我按下它没有任何反应时,有人可以帮助或解释为什么这不起作用?我不明白,它应该工作!



This should make the textfeild/text box in the html website say "HI" but when i press it nothing happens, can some one please help or explain why this isn''t working? I don''t understand, it should work!

推荐答案

好的,所以我相信我理解你的问题。您正在将一些html加载到您的Web浏览器组件中,但是您在问题中指定的文本输入没有使用HI填充。



从我看到/写的内容是因为文件在尝试使用HI填充文本框时尚未完成加载。因此,通过使用DocumentCompleted事件处理程序,将导致Web浏览器等待文档正确加载,然后运行您的代码以填充文本框。



我使用谷歌例如。



Ok so i believe i understand your question. You were loading some html into your web browser component but the text input was not being populated with HI as you specify in your question.

From what i saw/wrote it is because the document had not finished loading by the time it was trying to populate the textbox with HI. So by using the DocumentCompleted Event Handler that will cause the web browser to wait till the document has loaded properly and then run your code to populate the textbox.

I use google as the example.

public Form1()
        {
            InitializeComponent();
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        }

        private void AddText_Click(object sender, EventArgs e)
        {
            webBrowser1.Url = new Uri(@"http://google.com");
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            foreach (HtmlElement he in webBrowser1.Document.All.GetElementsByName("q"))
            {
                he.SetAttribute("value", "HI");
            }
        }







这就是你要找的?我理解它的方式我相信如此。




Is this what you were looking for? The way i understand it i believe so.


将class =pmain更改为id =pmain然后使用
change class="pmain" to id="pmain" then use
document.GetElementByID("pmain").SetValue("value","HI");


使用html按钮创建一个javascript函数









制作输入文本框服务器端
Either use a html button and create a javascript function

or


make the input textbox server side


这篇关于C#html元素更改输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆