Visual Studio .NET - No Focus from onload event

tom shad posted at 25-Jul-07 08:25
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

Click here to sign in and reply. You could earn money via our message board contest just for being helpful.
  No Focus from onload event - tom shad  25-Jul-07 08:25 8:25:46 PM
      Do a View Source on the rendered page in the browser. - Peter Bromberg  25-Jul-07 08:57 8:57:19 PM
          I did and did get it to work but not as I wanted - tom shad  25-Jul-07 09:15 9:15:01 PM
              if you r using usercontrol then do as under - Shail Kadiya  25-Jul-07 10:23 10:23:30 PM
                  Not sure why I need to do IsStartupScriptRegistered - tom shad  26-Jul-07 12:33 12:33:07 PM
                      ok let me answer one by one - Shail Kadiya  26-Jul-07 07:24 7:24:55 PM
                          Sounds Good - tom shad  27-Jul-07 12:16 12:16:33 PM
View Posts

  

Search

search



Purchase