By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,660 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,660 IT Pros & Developers. It's quick & easy.

setting focus to an asp.net web user control

P: n/a
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?
Nov 19 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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
--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:12**********************************@microsof t.com...
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?

Nov 19 '05 #2

P: n/a
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>
"CLEAR-RCIC" wrote:
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?

Nov 19 '05 #3

P: n/a
Huh!!, your user control id is "WebUserControl11" not uc1, so it should be
var control = document.getElementById("WebUserControl11:TextBox2 ");
--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
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>
"CLEAR-RCIC" wrote:
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?

Nov 19 '05 #4

P: n/a
I had tried that to. That doesn't work work either.

"CLEAR-RCIC" wrote:
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?

Nov 19 '05 #5

P: n/a

Well, how about posting the html rendered by the way, that way we can find
out what the textbox2 is rendering as
--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
I had tried that to. That doesn't work work either.

"CLEAR-RCIC" wrote:
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?

Nov 19 '05 #6

P: n/a
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>
"Kumar Reddi" wrote:

Well, how about posting the html rendered by the way, that way we can find
out what the textbox2 is rendering as
--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
I had tried that to. That doesn't work work either.

"CLEAR-RCIC" wrote:
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?


Nov 19 '05 #7

P: n/a
Hmm as you can see it rendered as "WebUserControl11_TextBox2 " instead of
the colon ":". So use the WebUserControl11_TextBox2 to reference it

--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:23**********************************@microsof t.com...
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>
"Kumar Reddi" wrote:

Well, how about posting the html rendered by the way, that way we can find out what the textbox2 is rendering as
--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
I had tried that to. That doesn't work work either.

"CLEAR-RCIC" wrote:

> 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?


Nov 19 '05 #8

P: n/a


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

--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:23**********************************@microsof t.com...
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>
"Kumar Reddi" wrote:

Well, how about posting the html rendered by the way, that way we can find out what the textbox2 is rendering as
--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
> I had tried that to. That doesn't work work either.
>
> "CLEAR-RCIC" wrote:
>
> > 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?


Nov 19 '05 #9

P: n/a
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?

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

--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:23**********************************@microsof t.com...
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>
"Kumar Reddi" wrote:

Well, how about posting the html rendered by the way, that way we can find out what the textbox2 is rendering as
--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
> I had tried that to. That doesn't work work either.
>
> "CLEAR-RCIC" wrote:
>
> > 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?


Nov 19 '05 #10

P: n/a

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
--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:6A**********************************@microsof t.com...
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?

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

--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:23**********************************@microsof t.com...
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>
"Kumar Reddi" wrote:

>
> Well, how about posting the html rendered by the way, that way we
can find
> out what the textbox2 is rendering as
> --
> Kumar Reddi
> http://kumarreddi.blogspot.com
>
> "CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
> news:EB**********************************@microsof t.com...
> > I had tried that to. That doesn't work work either.
> >
> > "CLEAR-RCIC" wrote:
> >
> > > 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?
>
>
>


Nov 19 '05 #11

P: n/a
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.

"Kumar Reddi" wrote:

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
--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:6A**********************************@microsof t.com...
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?

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

--
Kumar Reddi
http://kumarreddi.blogspot.com

"CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
news:23**********************************@microsof t.com...
> 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>
>
>
> "Kumar Reddi" wrote:
>
> >
> > Well, how about posting the html rendered by the way, that way we can find
> > out what the textbox2 is rendering as
> > --
> > Kumar Reddi
> > http://kumarreddi.blogspot.com
> >
> > "CLEAR-RCIC" <CL*******@discussions.microsoft.com> wrote in message
> > news:EB**********************************@microsof t.com...
> > > I had tried that to. That doesn't work work either.
> > >
> > > "CLEAR-RCIC" wrote:
> > >
> > > > 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?
> >
> >
> >


Nov 19 '05 #12

P: n/a
Did it work now?
Patrick
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.