Set focus on a TextBox

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Set focus on a TextBox

    How can I set focus on a TextBox from within a button's
    event handler.
    mg Guest

  2. Similar Questions and Discussions

    1. Popup; show, take focus, wait for button action, give focus, hide
      Hi. I've got a popup MC for displaying a message or asking a yes/no question that has to do the following on an event: - show and take focus...
    2. focus/lost focus of custom control
      Hi new to .NET and have a question. What I want to do if have a custome control (contains a table grid of text boxes) and multiple instances...
    3. setting focus to textbox
      Hello, Can someone tell me if/how I can set the focus to a textbox so when the page loads the cursor is placed in the textbox and the user can...
    4. Setting focus on TextBox using User Control
      Private Sub SetFocus(ByVal FocusControl As Control) Dim Script As New System.Text.StringBuilder Dim ClientID As String = FocusControl. ClientID ...
    5. VERY STRANGE BUG? Adding a textbox control causes other textbox control to fail???
      I've had this happen a few times too. I'm still not positive of what exactly causes it but all that's happening is the control that no longer...
  3. #2

    Default Re: Set focus on a TextBox

    Here's a little script I got, and modified, from Charles Carroll, i think,
    ([url]www.learnasp.com[/url]).

    Public Shared Sub SetFocus(ByVal ControlClientID As String, ByVal Page
    As System.Web.UI.Page)
    Dim scriptFunction As New System.Text.StringBuilder
    Dim scriptClientId As String
    scriptFunction.Append("<script language='javascript'>")
    scriptFunction.Append("document.getElementById('")
    scriptFunction.Append(ControlClientID)
    scriptFunction.Append("').focus();")
    scriptFunction.Append("</script>")
    Page.RegisterStartupScript("focus", scriptFunction.ToString())
    End Sub

    ControlClientID is the ClientID property of your control (likely a textbox).

    Karl


    "mg" <mg@theworld.com> wrote in message
    news:084701c34a0e$91af53d0$a401280a@phx.gbl...
    > How can I set focus on a TextBox from within a button's
    > event handler.

    Karl Seguin Guest

  4. #3

    Default Re: Set focus on a TextBox

    VB
    Sub SetFocus(ByVal controlToFocus As Control)

    Dim scriptFunction As New StringBuilder
    Dim scriptClientId As String

    scriptClientId = controlToFocus.ClientID

    scriptFunction.Append("<script language='javascript'>")
    scriptFunction.Append("document.getElementById('")
    scriptFunction.Append(scriptClientId)
    scriptFunction.Append("').focus();")
    scriptFunction.Append("</script>")

    RegisterStartupScript("focus", scriptFunction.ToString())

    End Sub

    C# (something like this)
    Fucntion SetFocus(Control controlToFocus )
    {
    String scriptFunction;
    String scriptClientId;

    scriptClientId = controlToFocus.ClientID;

    scriptFunction += ("<script language='javascript'>");
    scriptFunction. += ("document.getElementById('");
    scriptFunction. += (scriptClientId);
    scriptFunction. += ("').focus();");
    scriptFunction.Append("</script>");

    RegisterStartupScript("focus", scriptFunction.ToString());

    }


    dj Bass Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139