Find Control using javascript inside an ASP:Login control

Murtaza picture Murtaza · Sep 29, 2011 · Viewed 9.3k times · Source

I have an ASP Login control box on the page which is customized.

Inside the Login control we have Username and password textboxes.

I want to find username and password controls with a javascript function.

var Username= document.getElementById("<%=UserName.ClientID%>");

But this code does not compile and gives compile time error

UserName not found in this context.

and if I write the client side id :

var username = document.getElementById("login_LoginUser_UserName");

it executes properly, but I want to find the client id rather than using a hardcoded id here.

Answer

Samich picture Samich · Sep 29, 2011

The only way which I know is:

 var Username = document.getElementById("<%= Login1.FindControl("UserName").ClientID %>");
 var Password = document.getElementById("<%= Login1.FindControl("Password").ClientID %>");

It will returns the client Id of the controls inside of Login control.