Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. TextBox to autopostback "automatically" when text.length = x

TextBox to autopostback "automatically" when text.length = x

Scheduled Pinned Locked Moved ASP.NET
javascript
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    suni_dotnet
    wrote on last edited by
    #1

    Hi All, i have 4 textboxes. when the length of text in first textbox is equal to 4 then it should "automatically" change its focus to next textbox. i have set the autopostback to true but i need to click on screen to make a postback. i used onKeyPress, onKeyUp, onTextChanged but nothing is working for me... or is there any mistake in my code.. .aspx page : javascript code : function textLength() { var val = document.getElementById("TextBox2").value; if (val.length == 4) { document.getElementById("TextBox3").Focus(); } } in .aspx.cs page: TextBox2.Attributes.Add("onKeyUp", "javascript: textLength();"); is there any way to achieve this.. Thanks in Advance.

    C 1 Reply Last reply
    0
    • S suni_dotnet

      Hi All, i have 4 textboxes. when the length of text in first textbox is equal to 4 then it should "automatically" change its focus to next textbox. i have set the autopostback to true but i need to click on screen to make a postback. i used onKeyPress, onKeyUp, onTextChanged but nothing is working for me... or is there any mistake in my code.. .aspx page : javascript code : function textLength() { var val = document.getElementById("TextBox2").value; if (val.length == 4) { document.getElementById("TextBox3").Focus(); } } in .aspx.cs page: TextBox2.Attributes.Add("onKeyUp", "javascript: textLength();"); is there any way to achieve this.. Thanks in Advance.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      What do you mean by 'not working' ? I'd write a method that takes the id of the textbox you want to move to, the textbox you're in, and the length you're looking for, so you can reuse it. onkeyup="myfunc(this)" would pass the control instance, then you only need to look up the other one. Make sure that the client side id is 'TextBox2', it probably isn't. You use the ClientID property of the server control to get the client side Id. Never use a control you got with getElementById without checking first if it's null. You can also install firebug for firefox and step through your script to make sure it's called, and see how it's working. Or use alerts to tell you when they get called, if you want something less functional but easier to set up.

      Christian Graus Driven to the arms of OSX by Vista.

      S 1 Reply Last reply
      0
      • C Christian Graus

        What do you mean by 'not working' ? I'd write a method that takes the id of the textbox you want to move to, the textbox you're in, and the length you're looking for, so you can reuse it. onkeyup="myfunc(this)" would pass the control instance, then you only need to look up the other one. Make sure that the client side id is 'TextBox2', it probably isn't. You use the ClientID property of the server control to get the client side Id. Never use a control you got with getElementById without checking first if it's null. You can also install firebug for firefox and step through your script to make sure it's called, and see how it's working. Or use alerts to tell you when they get called, if you want something less functional but easier to set up.

        Christian Graus Driven to the arms of OSX by Vista.

        S Offline
        S Offline
        suni_dotnet
        wrote on last edited by
        #3

        Hi, thanks for replying... my javascript is not executing . i verified my code many times. iam unable to figure it out (why javascript is not executing). this is my entire code: protected void page_load(object sender, EventArgs e) { if(Page.IsPostBack) { TextBox2.Attributes.Add("onKeyUp", "javascript: textLength();"); } } ]]> Untitled Page function textLength() { alert("its called"); var val = document.getElementById("TextBox1").value; if (val.length == 4) { document.getElementById("TextBox2").Focus(); } }

        onKeyUp="textLength(this)" >

        Is everything is correct ?

        C 1 Reply Last reply
        0
        • S suni_dotnet

          Hi, thanks for replying... my javascript is not executing . i verified my code many times. iam unable to figure it out (why javascript is not executing). this is my entire code: protected void page_load(object sender, EventArgs e) { if(Page.IsPostBack) { TextBox2.Attributes.Add("onKeyUp", "javascript: textLength();"); } } ]]> Untitled Page function textLength() { alert("its called"); var val = document.getElementById("TextBox1").value; if (val.length == 4) { document.getElementById("TextBox2").Focus(); } }

          onKeyUp="textLength(this)" >

          Is everything is correct ?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          I think onkeyup should be all lowercase.

          Christian Graus Driven to the arms of OSX by Vista.

          S 1 Reply Last reply
          0
          • C Christian Graus

            I think onkeyup should be all lowercase.

            Christian Graus Driven to the arms of OSX by Vista.

            S Offline
            S Offline
            suni_dotnet
            wrote on last edited by
            #5

            still its not working... any other suggestions.... thanks

            A C 2 Replies Last reply
            0
            • S suni_dotnet

              still its not working... any other suggestions.... thanks

              A Offline
              A Offline
              Alok Sharma ji
              wrote on last edited by
              #6

              why should'nt you tried it in c# ontextchanged event and still if you want your javascrit running are you sure you checked advance browser settings to enable javascript (are you disabled them???? :-O ) or maybe this onkeyup="texlenght()" need a ";" or onkeyup="textlenght();" hope this should help.

              1 Reply Last reply
              0
              • S suni_dotnet

                still its not working... any other suggestions.... thanks

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                Make sure onkeyup is a javascript event, and look at the HTML that's being generated to see if it looks the way you'd expect.

                Christian Graus Driven to the arms of OSX by Vista.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups