Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my html code

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


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

C#
private void AddNameBtn_Click(object sender, EventArgs e)
     {
         foreach (HtmlElement he in webBrowser1.Document.All.GetElementsByName("MESSAGE"))
         {
                 he.SetAttribute("value", "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!
Posted
Comments
Prasad Khandekar 19-May-13 6:34am    
Is this a windows forms or WPF or ASP.NET application. For ASP.NET application your button click function will simply be transformed into

private void AddNameBtn_Click(object sender, EventArgs e) {
MESSAGE.Text = "Hi";
}
[no name] 19-May-13 6:37am    
its a .net application, and no that dosen't make sense, the MESSAGE.text, becasuse MESSAGE is not declared, this site is being loaded into a webbrowser1, and the finding the textfeild then adding text to it
[no name] 19-May-13 8:10am    
"nothing happens" is not helpful information. What does "nothing happens" mean? The button click event is not firing? Your foreach does not run? Can't find MESSAGE? You wanted your car to start and "nothing happens"?
[no name] 19-May-13 18:32pm    
well I don't know what the problem is, that wht I said nothing happens, because nothing happens, its like you fart and and you hear no sound
David_Wimbley 19-May-13 12:32pm    
I threw your code into a test project and it works for me.

Stupid question, but are you sure you are loading the HTML into your web browser component properly? Example: webBrowser1.Url = new Uri(@"c:\users\david\documents\visual studio 2012\Projects\Winforms1\Winforms1\Examples\test.html");

Otherwise with more details of what nothing happens means then im not sure what the problem is as it appears to work.

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.

C#
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.
 
Share this answer
 
Comments
[no name] 20-May-13 2:30am    
its worked, your a god!!!
change class="pmain" to id="pmain" then use
C#
document.GetElementByID("pmain").SetValue("value","HI");
 
Share this answer
 
Either use a html button and create a javascript function

or


make the input textbox server side
 
Share this answer
 
Comments
David_Wimbley 19-May-13 12:30pm    
I believe OP is doing this via winforms/web browser component
[no name] 19-May-13 18:36pm    
not much information

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900