I almost got this to work.
I can access the <body ID="myBody" runat="server"> tag from inside my User control and add an onLoad event to the 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.
I tried to use:
a.Attributes.Add("onLoad","addForm.LoginID.UserName.focus()")
But got an error that said that LoginID wasn't a member of addForm. I then tried to loop through the addForm elements and found the Name = LoginID:txtUserName. I changed the line to:
a.Attributes.Add("onLoad","addForm.LoginID:txtUserName.focus()")
I don't get the error now, but it still doesn't seem to work. It is still not getting focus.
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","addForm.LoginID:txtUserName.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" /> **********************************************
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 |