views:

37

answers:

2

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

+1  A: 

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";

Edit #1: 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

Nico
thanks but I don't have access to edit the HTML on the website :(
Nabisco_DING
see my edit number one
Nico
@Nico: I think he's just trying to post to a different website that isn't under his control
Chris Lively
thanks I will try this out and get back to you tonite!!
Nabisco_DING
A: 

I guess this isn't "doing it programatically with C#", but you could jQuerify the page, then run some custom javascript afterwards, to manipulate the value of the control. If you are using WebBrowser, you could call the below method to insert the scripts.

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

jQuerify code

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

Custom code

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