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. Setting Focus on a TextBox and sellecting the text in a DataGrid with TextChanged

Setting Focus on a TextBox and sellecting the text in a DataGrid with TextChanged

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
4 Posts 2 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.
  • T Offline
    T Offline
    thomasa
    wrote on last edited by
    #1

    I have a datagrid containing 2 coulmns with 1 TextBox in each. On the first TextBox I have a TextChanged event. After the event is fired I want to set Focus on the next textBox, and sellect the text on it. private void OnSubscriptionToValueChanged(object sender, System.EventArgs e) { TextBox tb = (TextBox)sender; if (tb.Text.Trim() != "VAT") { tb.CssClass = "tbStyle0"; DataGridItem dgi = (DataGridItem)tb.Parent.Parent; TextBox tbToFocus = (TextBox)dgi.FindControl("tbNextValue"); tbToFocus.Focus(); // Here it sets focus but it don't select the test } else { tb.Focus(); // Here it sets focus but it don't select the test tb.CssClass = "tbStyleError"; } } The code over sets Focus on the control I want, but it don't select the text (as it would if the TextChanged event wasen't there when you tab from one TextBox too another). Has anyone an idèe how to do that? Thanks Thomas

    T 1 Reply Last reply
    0
    • T thomasa

      I have a datagrid containing 2 coulmns with 1 TextBox in each. On the first TextBox I have a TextChanged event. After the event is fired I want to set Focus on the next textBox, and sellect the text on it. private void OnSubscriptionToValueChanged(object sender, System.EventArgs e) { TextBox tb = (TextBox)sender; if (tb.Text.Trim() != "VAT") { tb.CssClass = "tbStyle0"; DataGridItem dgi = (DataGridItem)tb.Parent.Parent; TextBox tbToFocus = (TextBox)dgi.FindControl("tbNextValue"); tbToFocus.Focus(); // Here it sets focus but it don't select the test } else { tb.Focus(); // Here it sets focus but it don't select the test tb.CssClass = "tbStyleError"; } } The code over sets Focus on the control I want, but it don't select the text (as it would if the TextChanged event wasen't there when you tab from one TextBox too another). Has anyone an idèe how to do that? Thanks Thomas

      T Offline
      T Offline
      thomasa
      wrote on last edited by
      #2

      Page.RegisterStartupScript("focus", "<script>document.getElementById('" + tbToFocus.UniqueID + "').focus();</script>"); Page.RegisterStartupScript("select", "<script>document.getElementById('" + tbToFocus.UniqueID + "').select();</script>");

      T 1 Reply Last reply
      0
      • T thomasa

        Page.RegisterStartupScript("focus", "<script>document.getElementById('" + tbToFocus.UniqueID + "').focus();</script>"); Page.RegisterStartupScript("select", "<script>document.getElementById('" + tbToFocus.UniqueID + "').select();</script>");

        T Offline
        T Offline
        thomasa
        wrote on last edited by
        #3

        Visual Studio 2005 says that the function is absolite, so one should rather use: Page.ClientScript.RegisterStartupScript(GetType(), "focus", "<script>document.getElementById('" + tbToFocus.UniqueID + "').focus();</script>"); Page.ClientScript.RegisterStartupScript(GetType(), "select", "<script>document.getElementById('" + tbToFocus.UniqueID + "').select();</script>"); Thomas

        K 1 Reply Last reply
        0
        • T thomasa

          Visual Studio 2005 says that the function is absolite, so one should rather use: Page.ClientScript.RegisterStartupScript(GetType(), "focus", "<script>document.getElementById('" + tbToFocus.UniqueID + "').focus();</script>"); Page.ClientScript.RegisterStartupScript(GetType(), "select", "<script>document.getElementById('" + tbToFocus.UniqueID + "').select();</script>"); Thomas

          K Offline
          K Offline
          Kelly Herald
          wrote on last edited by
          #4

          And to expand on your solution. You should just strip off the <script> tags and add a fourth parameter with the value of true. This will ensure that the <script> tags are added automatically with the type attribute of the tag set to text/javascript which will make the output XHTML compliant. Page.ClientScript.RegisterStartupScript(GetType(), "focus", "document.getElementById('" + tbToFocus.UniqueID + "').focus();", true); Page.ClientScript.RegisterStartupScript(GetType(), "select", "document.getElementById('" + tbToFocus.UniqueID + "').select();", true);

          Kelly Herald Software Developer

          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