Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > setting focus to an asp.net web user control

Reply
Thread Tools

setting focus to an asp.net web user control

 
 
=?Utf-8?B?Q0xFQVItUkNJQw==?=
Guest
Posts: n/a
 
      12-06-2004
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?
 
Reply With Quote
 
 
 
 
Kumar Reddi
Guest
Posts: n/a
 
      12-06-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?



 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q0xFQVItUkNJQw==?=
Guest
Posts: n/a
 
      12-06-2004
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?

 
Reply With Quote
 
Kumar Reddi
Guest
Posts: n/a
 
      12-06-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?



 
Reply With Quote
 
=?Utf-8?B?Q0xFQVItUkNJQw==?=
Guest
Posts: n/a
 
      12-06-2004
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?

 
Reply With Quote
 
Kumar Reddi
Guest
Posts: n/a
 
      12-06-2004

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?



 
Reply With Quote
 
=?Utf-8?B?Q0xFQVItUkNJQw==?=
Guest
Posts: n/a
 
      12-06-2004
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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?

>
>
>

 
Reply With Quote
 
Kumar Reddi
Guest
Posts: n/a
 
      12-06-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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?

> >
> >
> >



 
Reply With Quote
 
=?Utf-8?B?Q0xFQVItUkNJQw==?=
Guest
Posts: n/a
 
      12-06-2004


"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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > 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?
> > >
> > >
> > >

>
>
>

 
Reply With Quote
 
=?Utf-8?B?Q0xFQVItUkNJQw==?=
Guest
Posts: n/a
 
      12-06-2004
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > 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?
> > >
> > >
> > >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
emulating window.focus in Body onload() event and setting focus to a control on same page Jason ASP .Net 4 05-07-2007 05:54 PM
Setting Focus in Web User Control that Uses AJAX Update Panel Colin Halliday ASP .Net Web Controls 2 04-24-2007 03:40 AM
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
setting the focus to a textbox in a user control =?Utf-8?B?U3JpZGhhcg==?= ASP .Net 2 02-10-2006 04:49 AM
Setting focus to another User Control MattB ASP .Net 0 09-27-2005 10:20 PM



Advertisments