How do you set the value of an input element programmatically through CSharp?

Hi I'm trying to automate my IE to enter a website, but the problem is that the input elements do not have an HTML ID attribute! For instance:

<input type="text" name="user" size="15" value="">

How do you program C # to insert text in this text box?

thank

+5
source share
5 answers

Add the following attributes to your input tag: runat="server"andid="someId"

<input id="user" type="text" size="15" runat="server">

Then the server side:

user.Text = "sample text";

Then you can do something like:

foreach (Control c in Page.Controls)
{
    TextBox t = c as TextBox;

    if (t != null)
    {
        t.Text = "sample text";
    }
}

But I'm not sure that it will work without an attribute runat="server"

+6
source

I know this is a little late, but here is an alternative to the jquery method.

, IE -. , .

-

HtmlElementCollection inputs = browser.Document.GetElementsByTagName("input"); >

. input.GetAttribute("name").Equals("user") >

input.SetAttribute("value", "MyUserName"); >

+4

, " #", jQuerify , javascript , . WebBrowser, , .

string script = "script text";
WebBrowser.Navigate(script);

jQuerify

var s=document.createElement('script');
s.setAttribute('src','http://jquery.com/src/jquery-latest.js');
document.getElementsByTagName('body')[0].appendChild(s);

$(document).ready(function(){$('input[type="text"][name="user"]').val('FooBar')});
+2

, : -

  • NB: http://msdn.microsoft.com/en-us/library/2te2y1x6.aspx http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.aspx http://social.msdn.microsoft.com/Search/en-US/?query=mshtml%20tutorial&ac=1

  • , , Windows,

  • MSHTML, .. Microsoft HTML SHDocVw, Microsoft Internet Controls,

  • - :

            /*INTERNET EXPLORER OBJECT*/
            SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
    ie.Navigate("http://www.example.com/entry"); /*GO TO EXAMPLE.COM*/
            /*WAIT UNTIL THE BROWSER IS READY AND COMPLETELY LOADED*/
    while (ie.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE) 
            {
               Application.DoEvents();
            }
            mshtml.HTMLDocument doc = ie.Document;
            while (doc.readyState != "complete")
            {
               Application.DoEvents();
            }
    /*GET ALL THE INPUT ELEMETS IN A COLLECTION*/
    MSHTML.IHTMLElementCollection collection=
            doc.getElementsByTagName("INPUT");
            foreach (mshtml.IHTMLElement elem in collection)
            {
              if (elem.getAttribute("name") != null)
                {
                  /*IDENTIFY THE INPUT CONTROL BY NAME ATTRIBUTE*/
          if (elem.getAttribute("name").Equals("user"))
                  {/*ENTER USER NAME*/
                   elem.setAttribute("value", "ABC");
          }
        }
    }                           
    
+2

:

<input id="user" type="text" size="15" runat="server">

:

user.Value = "sample text";

Insted!

+1

All Articles