We help IT Professionals succeed at work.
Get Started

Focus on text box

jknj72
jknj72 asked
on
145 Views
Last Modified: 2016-04-14
I have an app that was built in an earlier version of ASP.NET 1.1. I have upgraded most of the app to 4.0 (HTML5) and I am trying to set focus to a textbox when a form loads but ever since I have upgraded to IE 11 this is not working. The previous developer has a script on the server side to set the focus to the txtName textbox and injects it before the page renders. FYI, I  added a ScriptManager because I added an AJAX control so if that comes into play please let me know

Here is the call to the function in the markup in the body tag.
	<body bgColor="#ffffff" background="./images/testbg.gif" style="margin-top:0" MS_POSITIONING="GridLayout" [b]onload="sf()"[/b]>

Open in new window


Then in the code behind this is what is running but like I said, in IE 11 this does not work(all other versions and browsers seem to be working fine).
'insert missing tags in the default markup
    Private Function FixMarkup(ByVal gridMarkup As String) As String
        'local variables
        Dim StrScript As String
       [b] If rbtnNameSearch.Checked = True Then
            StrScript = "function sf()" _
            & " { " _
            & " document.Form1.txtName.focus(); " _
            & " }"
        Else
            StrScript = "function sf()" _
            & " { " _
[u]            & " document.Form1.txtFileNumber.focus(); " _[/u]
            & " } " _
            & "function validateRange(source,Args){" _
            & "if (Args.Value > 999 && Args.Value < 999999)" _
            & "  Args.IsValid = true;" _
            & "else" _
            & "  Args.IsValid = false;" _
            & " return;" _
            & "}"[/b]
        End If
        'find the 1st occurrence of <head> and insert <link href="MainCss.css" type="text/css" rel="stylesheet">
        Dim insertpoint As Integer
        insertpoint = gridMarkup.IndexOf("language=""javascript"">") + ("language=""javascript"">").Length
        gridMarkup = gridMarkup.Insert(insertpoint, StrScript)

        Return gridMarkup
    End Function

Open in new window


The underlined piece of code above is what Im trying to get to work. I have tried numerous different types of code snippets but nothing has worked. I actually got it to work momentarily using the .ready function in jquery but have since lost the code and cant get it to work again. I have also tried to add a newer version of a jquery file(jquery-1.12.1.min.js) to see if that would help but it hasnt. I have tried multiple functions like these

1. in place of the underlined code above
 ' // Reference the textbox
    var txt = document.getElementById("<%=txtName.ClientID%>");  'also tried txtName without ClientID

'    // If the textbox was found, call its focus function
     if (txt != null)
     txt.focus()

2. Also Tried all of these in the markup in the <Head> tag
        
<script src="Scripts/jquery-1.12.1.min.js"></script>    
        <script type="text/javascript">
            $(function () {
                $("#txtName").focus(function () {
                    alert("hello world");
                })
            })
        </script>

         <script type="text/javascript" >
             $(document).ready(function () {
                 $("#txtName").focus();
             });
		</script>

        <script type="text/javascript" language="javascript">
            function TestFocus() {
                // Reference the textbox
                var txt = document.getElementById("<%=txtName.ClientID%>");

                // If the textbox was found, call its focus function
                if (txt != null)
                    txt.focus()
                else
                    alert("Nothing here");
            }
		</script>

Open in new window


I am curious if maybe I could do this in the CSS instead? Anything that will get this to work I am willing to try. Just a thought...

I must tell you that I am not a javascript/jqeury guy(but I am learning) so its kinda foreign to me so if I am missing something simple dont be surprised. I have been on this for a few days now and need to move on so please help me out. It would be greatly appreciated.

Thanks
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE