I almost got this to work.
I can now access the <body ID="myBody" runat="server"> tag. When I add the
onLoad event to the myBody tag - it is there, but the control still doesn't
get focus. I named the User Control to make sure I can get to it, but it
still doesn't work.
Here are the 2 files:
test.aspx:
**********************************************
<%@ Page Language="VB" trace="false" 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>
</head>
<body id="myBody" runat="server" >
<form id="addForm" runat="server">
<fts:Logon ID="LoginID" runat="Server"/>
</form>
</body>
</html>
***********************************************
test.ascx:
***********************************************
<script 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","document.forms[0].LoginID.UserName.focus()")
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" />
**********************************************
I am getting an error that says:
Error: "document.forms.0.LoginID.UserName" is null or not an object
I am confused here. Isn't LoginID part of forms[0]? And isn't UserName part
of LoginID???? Or is it how I am accessing it?
I also tried to changed the Forms ID to "addForm" in the Attributes line to:
a.Attributes.Add("onLoad","document.addForm.LoginID.UserName.focus()")
and get the same error except the forms.0 is replaced with addform.
Here is the Tree from the Trace:
__PAGE ASP.test_aspx
_ctl0 System.Web.UI.LiteralControl
_ctl1 System.Web.UI.LiteralControl
myBody System.Web.UI.HtmlControls.HtmlGenericControl
_ctl2 System.Web.UI.LiteralControl
addForm System.Web.UI.HtmlControls.HtmlForm
_ctl3 System.Web.UI.LiteralControl
LoginID ASP.test_ascx
LoginID:txtUserName System.Web.UI.WebControls.TextBox
LoginID:_ctl1 System.Web.UI.LiteralControl
LoginID:_ctl0
System.Web.UI.WebControls.RequiredFieldValidator
LoginID:_ctl2 System.Web.UI.LiteralControl
_ctl4 System.Web.UI.LiteralControl
_ctl5 System.Web.UI.LiteralControl
_ctl6 System.Web.UI.LiteralControl
Thanks,
Tom
I tried looped through the Form elements and found that the name of the
textbox is actually - LoginID:txtUserName. I change the attribute lines to:
Dim a as htmlControl
a = CType(Page.FindControl("myBody"),htmlControl)
a.Attributes.Add("onLoad","onload=document.addForm.LoginID:txtUserName.focus();")
I am not getting there error now, but I am also not getting focus.
What is missing here?
Thanks,
Tom
>I almost got this to work.
>
[quoted text clipped - 152 lines]
>>
>> Tom
tshad - 26 Jul 2007 01:53 GMT
Ok, I got it to work. But not the way I wanted.
As well as the one below I tried:
a.Attributes.Add("onLoad","onload=addForm.LoginID:txtUserName.focus();")
and
a.Attributes.Add("onLoad","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" %>
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
if not IsPostBack
Dim a as htmlControl
a = CType(Page.FindControl("addForm"),htmlControl)
For each frmCtrl as Control in a.Controls
trace.warn("frmCtrl = " & frmCtrl.GetType.ToString())
trace.warn("frmCtrl.ID = " & frmCtrl.ID)
Next
end if
end sub
</script>
<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","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?).
Thanks,
Tom
>I tried looped through the Form elements and found that the name of the
>textbox is actually - LoginID:txtUserName. I change the attribute lines
[quoted text clipped - 170 lines]
>>>
>>> Tom
Göran Andersson - 26 Jul 2007 12:20 GMT
> I tried looped through the Form elements and found that the name of the
> textbox is actually - LoginID:txtUserName.
That's odd. That name is not very usable. I would expect the name to be
LoginID$txtUserName and the id to be LoginID_txtUserName. That's how
controls are usually named.
What does the generated html code for the form look like?
> I change the attribute lines to:
>
> Dim a as htmlControl
> a = CType(Page.FindControl("myBody"),htmlControl)
> a.Attributes.Add("onLoad","onload=document.addForm.LoginID:txtUserName.focus();")
An identifier in Javascript can not contain a colon. If the name really
contains a colon, you would need to use a string to reference it:
onload=document.addForm.elements('LoginID:txtUserName').focus();
> I am not getting there error now, but I am also not getting focus.
The browser doesn't display Javscript errors by default. In Internet
Explorer they show up as a notice in the status bar. In Firefox you open
the Error Console to see them.

Signature
Göran Andersson
_____
http://www.guffa.com
tshad - 26 Jul 2007 16:59 GMT
>> I tried looped through the Form elements and found that the name of the
>> textbox is actually - LoginID:txtUserName.
[quoted text clipped - 4 lines]
>
> What does the generated html code for the form look like?
Here is the line with the input tag:
<input name="LoginID:txtUserName" type="text" size="25"
id="LoginID_txtUserName" />
<span id="LoginID__ctl0" controltovalidate="LoginID_txtUserName"
evaluationfunction="RequiredFieldValidatorEvaluateIsValid" initialvalue=""
style="color:Red;visibility:hidden;">User Name Required</span>
What is interesting is that the javascript uses the LoginID:txtUserName and
the ID uses the underscore. The trace shows the ":" which is the Name not
the ID.
__PAGE ASP.test_aspx
_ctl0 System.Web.UI.LiteralControl
_ctl1 System.Web.UI.ResourceBasedLiteralControl
myBody System.Web.UI.HtmlControls.HtmlGenericControl
_ctl2 System.Web.UI.LiteralControl
addForm System.Web.UI.HtmlControls.HtmlForm
_ctl3 System.Web.UI.LiteralControl
LoginID ASP.test_ascx
LoginID:_ctl1 System.Web.UI.LiteralControl
LoginID:txtUserName System.Web.UI.WebControls.TextBox
LoginID:_ctl2 System.Web.UI.LiteralControl
LoginID:_ctl0
System.Web.UI.WebControls.RequiredFieldValidator
LoginID:_ctl3 System.Web.UI.LiteralControl
_ctl4 System.Web.UI.LiteralControl
_ctl5 System.Web.UI.LiteralControl
_ctl6 System.Web.UI.LiteralControl
The code that works uses the ":" in the function just not in the inline code
as shown here:
function GetFocus()
{
if (document.getElementById("LoginID:txtUserName") != null)
{
document.getElementById("LoginID:txtUserName").focus();
}
}
Thanks,
Tom
>> I change the attribute lines to:
>>
[quoted text clipped - 13 lines]
> Explorer they show up as a notice in the status bar. In Firefox you open
> the Error Console to see them.