uwspstar:Step 1: to open the view source in your IE browser, you will find the exactly ID which the server assigned to your textbox1 ( something like ct1_txtId ...)
That is bad advice. Wherever possible don't hardcode these values into your code - update your user control name and *wham* your javascript breaks. Instead you could expose some public properties in your user control to return either the client id of your textbox or indeed a reference to the textbox itself, e.g. (in code-behind of your user control):
// Return the clientID of my textbox
public string TextBox1ClientID
{
get { return TextBox1.ClientID; }
}
// Return a reference to my textbox
public TextBox TextBox1Ref
{
get { return TextBox1; }
}
In the javascript of your page then you could get the client id and set focus in javascript:
window.onload = function(){
var ele = document.getElementById("<%=TestControl1.TextBox1ClientID%>"); // Use this if your public property returns just the client id of your textbox
var ele = document.getElementById("<%=TestControl1.TextBox1Ref.ClientID%>"); // Use this if your public property returns a reference to your textbox
ele.focus();
}
Alternatively if you wanted to set focus in the code-behind of your page you could create a public method in your user control which would do it for you:
-- User Control code-behind --
public void TextBox1SetFocus()
{
TextBox1.Focus();
}
-- Page code-behind (Page_Load or wherever appropriate) --
MyUserControl.TextBox1SetFocus()
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."