Thursday, March 29, 2012

LOGIN Controls & Setting Focus--NEED HELP PLEASE

Good morning friends,

I am working with Visual Studio 2005, ASP.NET 2.0

I am working with the Login controls provided my .NET 2.0, trying to make
the Login1 control UserName textbox obtain SetFocus upon load of the .aspx
web page but I cannot for the life of me get this to work. I tried using
the defaultfocus on the Form but that did not work.

My code in my MasterPage is as follow::::

<form runat=server id="aspLoginForm">
<script language='JavaScript' type="text/javascript">
<!--
function SetFocus()
{
document.aspLoginForm['Login1_UserName'].focus();
}
window.onload = SetFocus;
// -->
</script>
<div>
<br />
<br />
<br />
</div>
<br />
<br />
<br />
<br />
<br />
<asp:Login ID="Login1" runat="server"

Any ideas would be greatly apprecited.

Thanks in advance,

~Brad"Brad Isaacs" <bisaacs@dotnet.itags.org.rogers.comwrote in message
news:OCiLDR%23OHHA.3552@dotnet.itags.org.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

window.onload = SetFocus;


window.onload = SetFocus();
On Fri, 19 Jan 2007 11:09:55 -0500, "Brad Isaacs" <bisaacs@dotnet.itags.org.rogers.com>
wrote:

Quote:

Originally Posted by

>Good morning friends,
>
>I am working with Visual Studio 2005, ASP.NET 2.0
>
>I am working with the Login controls provided my .NET 2.0, trying to make
>the Login1 control UserName textbox obtain SetFocus upon load of the .aspx
>web page but I cannot for the life of me get this to work. I tried using
>the defaultfocus on the Form but that did not work.
>
>


I just did

Login1.Focus()

in Page_Load.

Is that what you mean?
Hey again,

Thanks MarK Rae for my typo on .setFocus()

Login1.focus in the Page Load references an NULL object.

I am trying to use the Javascript code inside my HTML

<form runat=server id="aspLoginForm">

<script language='JavaScript' type="text/javascript">

<!--

function SetFocus()

{

document.aspLoginForm['Login1_LayoutTemplate_UserName'].focus();

}

window.onload = SetFocus();

// -->

</script>

But it is not working at all............ So I thought I of using another
method..........

Any ideas?

Thanks in advance,

~Brad

"dgk" <dgk@dotnet.itags.org.somewhere.comwrote in message
news:j3t1r25i56nlv7ccaa1p9bqko000ebk3jq@dotnet.itags.org.4ax.com...

Quote:

Originally Posted by

On Fri, 19 Jan 2007 11:09:55 -0500, "Brad Isaacs" <bisaacs@dotnet.itags.org.rogers.com>
wrote:
>

Quote:

Originally Posted by

>>Good morning friends,
>>
>>I am working with Visual Studio 2005, ASP.NET 2.0
>>
>>I am working with the Login controls provided my .NET 2.0, trying to make
>>the Login1 control UserName textbox obtain SetFocus upon load of the .aspx
>>web page but I cannot for the life of me get this to work. I tried using
>>the defaultfocus on the Form but that did not work.
>>
>>


>
I just did
>
Login1.Focus()
>
in Page_Load.
>
Is that what you mean?


"Brad Isaacs" <bisaacs@dotnet.itags.org.rogers.comwrote in message
news:OzAO7q%23OHHA.3944@dotnet.itags.org.TK2MSFTNGP06.phx.gbl...

Quote:

Originally Posted by

<form runat=server id="aspLoginForm">


Ah yes, but you're using MasterPages aren't you...? Do a View Source and
you'll see that ASP.NET has (for reasons which I've never understood)
renamed your form to 'aspnetForm'

Are you even sure your JavaScript function is being called...? Try this:

alert('1');
document.getElementById('<%=UserName.ClientID%>').focus();
alert('2');

How many alerts do you get...?
On Fri, 19 Jan 2007 17:08:18 -0000, "Mark Rae"
<mark@dotnet.itags.org.markNOSPAMrae.comwrote:

Quote:

Originally Posted by

>"Brad Isaacs" <bisaacs@dotnet.itags.org.rogers.comwrote in message
>news:OzAO7q%23OHHA.3944@dotnet.itags.org.TK2MSFTNGP06.phx.gbl...
>

Quote:

Originally Posted by

><form runat=server id="aspLoginForm">


>
>Ah yes, but you're using MasterPages aren't you...? Do a View Source and
>you'll see that ASP.NET has (for reasons which I've never understood)
>renamed your form to 'aspnetForm'
>
>Are you even sure your JavaScript function is being called...? Try this:
>
>alert('1');
>document.getElementById('<%=UserName.ClientID%>').focus();
>alert('2');
>
>How many alerts do you get...?
>


I don't understand why he gets a null reference since I'm using a
masterpage also. This is my whole page_load for login.aspx:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
CType(Master, PRDUA).HideLogin()
Login1.Focus()
End Sub

The first line is just calling a function on the master page to hide
the login/logout link if you're on the login page. The second
successfully sets the focus to the login control.
If you get the two alerts but it's still not setting the focus, change
alert('1') to the word debugger; and remove alert('2'). If you are browsing
with Internet Explorer go to Tools menu, Internet Options, Advanced Tab and
remove the checkbox "Disable script debugging (Internet Explorer)".
Also, the second alert could cause the focus to be lost depending on the
browser you are using, probably best to change that one to window.status =
"2";
The word debugger in the client code will kick of client side debugging.

Mark's example is really a best practice especially if you are using user
controls or master pages as the control id will change depending on the
number of controls and / or the nesting.

I don't know what browser compatibility level you are looking for... but the
getElementById is the most compatible method; it will work on any DOM
capable browser and on all new browsers. Older browsers you would probably
need to-do a test something like this..

if (document.getElementById)
document.getElementById('<%=UserName.ClientID%>').focus();
else if (document.all)
document.all('<%=UserName.ClientID%>').focus();
else if (document.layers)
document.layers('<%=UserName.ClientID%>').focus();

Regards,
Brian K. Williams

"Mark Rae" <mark@dotnet.itags.org.markNOSPAMrae.comwrote in message
news:eMo2px%23OHHA.1248@dotnet.itags.org.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

"Brad Isaacs" <bisaacs@dotnet.itags.org.rogers.comwrote in message
news:OzAO7q%23OHHA.3944@dotnet.itags.org.TK2MSFTNGP06.phx.gbl...
>

Quote:

Originally Posted by

><form runat=server id="aspLoginForm">


>
Ah yes, but you're using MasterPages aren't you...? Do a View Source and
you'll see that ASP.NET has (for reasons which I've never understood)
renamed your form to 'aspnetForm'
>
Are you even sure your JavaScript function is being called...? Try this:
>
alert('1');
document.getElementById('<%=UserName.ClientID%>').focus();
alert('2');
>
How many alerts do you get...?
>


Hey again,

WOW -- the madness has ended.
The Javascript was not firing........ I could not see any alerts, I messed
around and found that if I took out the Javascript and used the Page_Load or
Login1_Load events, then it worked using "dgk" Login1.focus()

This worked:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Login1.Focus()

End Sub

Protected Sub Login1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Login1.Load

Login1.Focus()

End Sub

Onto my next adventure.......

Thank you both for all your help/input.......

Very much appreciated,

~BRad

"dgk" <dgk@dotnet.itags.org.somewhere.comwrote in message
news:eo12r2ttjaneetuvlutit5pmu95rg8ug5r@dotnet.itags.org.4ax.com...

Quote:

Originally Posted by

On Fri, 19 Jan 2007 17:08:18 -0000, "Mark Rae"
<mark@dotnet.itags.org.markNOSPAMrae.comwrote:
>

Quote:

Originally Posted by

>>"Brad Isaacs" <bisaacs@dotnet.itags.org.rogers.comwrote in message
>>news:OzAO7q%23OHHA.3944@dotnet.itags.org.TK2MSFTNGP06.phx.gbl...
>>

Quote:

Originally Posted by

>><form runat=server id="aspLoginForm">


>>
>>Ah yes, but you're using MasterPages aren't you...? Do a View Source and
>>you'll see that ASP.NET has (for reasons which I've never understood)
>>renamed your form to 'aspnetForm'
>>
>>Are you even sure your JavaScript function is being called...? Try this:
>>
>>alert('1');
>>document.getElementById('<%=UserName.ClientID%>').focus();
>>alert('2');
>>
>>How many alerts do you get...?
>>


>
I don't understand why he gets a null reference since I'm using a
masterpage also. This is my whole page_load for login.aspx:
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
CType(Master, PRDUA).HideLogin()
Login1.Focus()
End Sub
>
The first line is just calling a function on the master page to hide
the login/logout link if you're on the login page. The second
successfully sets the focus to the login control.


DGK,

Your code is running on the server and will resolve the client id. His code
is client side code and doesn't know what the client id will be. Mark's
example code changed that so he is getting the client id from the server.

Regards,
Brian K. Williams

"dgk" <dgk@dotnet.itags.org.somewhere.comwrote in message
news:eo12r2ttjaneetuvlutit5pmu95rg8ug5r@dotnet.itags.org.4ax.com...

Quote:

Originally Posted by

On Fri, 19 Jan 2007 17:08:18 -0000, "Mark Rae"
<mark@dotnet.itags.org.markNOSPAMrae.comwrote:
>

Quote:

Originally Posted by

>>"Brad Isaacs" <bisaacs@dotnet.itags.org.rogers.comwrote in message
>>news:OzAO7q%23OHHA.3944@dotnet.itags.org.TK2MSFTNGP06.phx.gbl...
>>

Quote:

Originally Posted by

>><form runat=server id="aspLoginForm">


>>
>>Ah yes, but you're using MasterPages aren't you...? Do a View Source and
>>you'll see that ASP.NET has (for reasons which I've never understood)
>>renamed your form to 'aspnetForm'
>>
>>Are you even sure your JavaScript function is being called...? Try this:
>>
>>alert('1');
>>document.getElementById('<%=UserName.ClientID%>').focus();
>>alert('2');
>>
>>How many alerts do you get...?
>>


>
I don't understand why he gets a null reference since I'm using a
masterpage also. This is my whole page_load for login.aspx:
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
CType(Master, PRDUA).HideLogin()
Login1.Focus()
End Sub
>
The first line is just calling a function on the master page to hide
the login/logout link if you're on the login page. The second
successfully sets the focus to the login control.

0 comments:

Post a Comment