How Do You Set Value of Input Element Programmatically Through CSharp?

Nabisco_DING picture Nabisco_DING · Oct 21, 2010 · Viewed 36.8k times · Source

Hello I am trying to automate my IE to log into a website but the problem is that the input elements do not have an HTML ID attribute! For example:

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

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

Thanks

Answer

bevacqua picture bevacqua · Oct 21, 2010

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

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

Then server-side you do:

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 it'll work without the runat="server" attribute