I did that as well as the Trace that was in the original post and found the ID = LoginID:txtUserName. So I don't need to make this variable public.
But I still couldn't get this to work directly from the <body> tag, as I would like. Shail put me in the right direction to get it to work calling a function - but I would think I should be able to call it directly.
Ok, I got it to work. But not the way I wanted.
I tried:
a.Attributes.Add("onLoad","addForm.LoginID:txtUserName.focus();")
and
a.Attributes.Add("onLoad","LoginID:txtUserName.focus();")
Neither one of these worked. There was no error but there was no focus,
either.
I made a change by calling a Javascript function from the onload function
and this worked. The LoginID:txtUserName is the correct name but why does
the above not work???
The files that work are:
test.aspx:
*************************************************
<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ Register TagPrefix="fts" TagName="Logon" src="test.ascx" %>
<html>
<head>
<title>Login</title>
<script language="JavaScript">
function GetFocus()
{
if (document.getElementById("LoginID:txtUserName") != null)
{
document.getElementById("LoginID:txtUserName").focus();
}
}
</script>
</head>
<body id="myBody" runat="server">
<form id="addForm" runat="server">
<fts:Logon ID="LoginID" runat="Server"/>
</form>
</body>
</html>
*************************************************
test.ascx:
*************************************************
<script language="vb" runat="server" >
Sub Page_Load(sender as Object, e as EventArgs)
dim IsCheckResumes as Boolean = true
if not IsPostBack
Dim a as htmlControl
a = CType(Page.FindControl("myBody"),htmlControl)
a.Attributes.Add("onLoad","GetFocus();")
end if
End Sub
Public Property UserName AS String
Get
Return txtUserName.Text
End Get
Set
txtUserName.Text = Value
End Set
End Property
</script>
<asp:textbox id="txtUserName" TextMode="SingleLine" Columns="25"
runat="server" />
<asp:RequiredFieldValidator
ControlToValidate="txtUserName"
Text="User Name Required"
runat="server" />
*************************************************
This works but I would rather do the focus directly from the <body> tag and
not have to build an extra function for each page that needs to have focus.
Can anyone explain to me why this isn't working? Does it have to do with
timing on when a control is put on the page? I would assume this wouldn't
be the case as the page is built before the onload function is called (isn't
it?).
I don't know of the other methods that are built into asp.net control to set default focus. When I was looking in this - this was the only method I was told about that worked when I wasn't dealing with a User Control. I am using Asp.net 1.1, BTW.
Thanks,
Tom