LOGIN Controls & Setting Focus--NEED HELP PLEASE

B

Brad Isaacs

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 />
&nbsp;</div>
<br />
<br />
<br />
<br />
<br />
<asp:Login ID="Login1" runat="server"


Any ideas would be greatly apprecited.

Thanks in advance,

~Brad
 
D

dgk

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

Brad Isaacs

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
 
M

Mark Rae

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

dgk

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.
 
B

Brian Williams

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
 
B

Brad Isaacs

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
 
B

Brian Williams

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
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top