setting focus to an asp.net web user control

G

Guest

Hi,

I'm having problems setting focus to a textbox on a web user contol on an
asp.net web page. The following script works on normal asp.net pages:

<script language="javascript">
function cmdButton1_Clicked()
{ document.all('txtInput1').focus();
return false;
}
</script>

But it fails when using web user controls. Anyone know how to set focus to
an object in this senario?
 
K

Kumar Reddi

When you usercontrols, the controls inside the userControls will have an id
like userContrlID:childControlID

So, lets say your user control has an id like "uc1"
and the textbox inside it has an id like "txtInput1"

Then your textbox's id after it is rendered to the web page becomes
"uc1:txtInput1"

So you should use

document.all('uc1:txtInput1').focus();

Let me know if its not clear. Good Luck
 
G

Guest

That doesn't seem to work either:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="FocusTester.WebForm1"%>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<script language="javascript">
var control = document.getElementById("uc1:TextBox2");
alert(control)
if( control != null ){control.focus();}
</script>

<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtInput1" runat="server"></asp:TextBox>
<br>
<uc1:WebUserControl1 id="WebUserControl11"
runat="server"></uc1:WebUserControl1>
</form>
</body>
</HTML>
 
K

Kumar Reddi

Huh!!, your user control id is "WebUserControl11" not uc1, so it should be
var control = document.getElementById("WebUserControl11:TextBox2");
 
K

Kumar Reddi

Well, how about posting the html rendered by the way, that way we can find
out what the textbox2 is rendering as
 
G

Guest

Good thinking. Here it is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0042)http://localhost/FocusTester/WebForm1.aspx -->
<HTML><HEAD><TITLE>WebForm1</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR>
<META content="Visual Basic .NET 7.1" name=CODE_LANGUAGE>
<META content=JavaScript name=vs_defaultClientScript>
<META content=http://schemas.microsoft.com/intellisense/ie5
name=vs_targetSchema>
<SCRIPT language=javascript>
var control = document.getElementById("WebUserControl11:TextBox2");
alert(control)
if( control != null ){control.focus();}
</SCRIPT>
</HEAD>
<BODY>
<FORM id=Form1 name=Form1 action=WebForm1.aspx method=post><INPUT type=hidden
value=dDwxNzE0Mjk4ODM7Oz5LkR2Ym7FGguoxTWTE3/K4xV30DA== name=__VIEWSTATE>
<INPUT
id=txtInput1 name=txtInput1> <BR><INPUT id=WebUserControl11_TextBox2
name=WebUserControl11:TextBox2> </FORM></BODY></HTML>
 
K

Kumar Reddi

Hmm as you can see it rendered as "WebUserControl11_TextBox2 " instead of
the colon ":". So use the WebUserControl11_TextBox2 to reference it
 
G

Guest

Kumar Reddi said:
Hmm as you can see it rendered as "WebUserControl11_TextBox2 " instead of
the colon ":". So use the WebUserControl11_TextBox2 to reference it
 
G

Guest

Yep. Tried that too. It doesn't work either. I'm putting this code on the
main WebForm1.aspx page. That's correct isn't it?
 
K

Kumar Reddi

Hmm there the problem is. I thought you had the code executing in response
to an event. Ok, as you can see the code is in the HEAD section. This code
executes before, the controls are even written to the page. So, your
javascript code is not able to find the control you are referring to, since
its not loaded yet. So,you should put this code after the closing FORM tag
and inside the closing BODY tag. At this position, the controls on the page
are loaded, but the form is not shown to the user yet. So, that should do
your work
 
G

Guest

Yup. That did it. I know that the code has to go after the contol is
rendered but had lost track after trying too many different things. Thanks
for all your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top