Re: Activate a text box?

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Alvin Bruney [MVP] (vapor)
Date: 07/02/04


Date: Fri, 2 Jul 2004 18:16:07 -0500

this is what i use. just pass in the control you want to set focus to. end
of story

  /// <param name="control">Control to set the InitialFocus on.</param>
  public static void SetInitialFocus(System.Web.UI.Control control)
  {
   if (control.Page == null)
   {
    throw new ArgumentException(
     "The Control must be added to a Page before you can set the IntialFocus
to it.");
   }
   if (control.Page.Request.Browser.JavaScript == true)
   {
    // Create JavaScript
    System.Text.StringBuilder s = new System.Text.StringBuilder();
    s.Append("\n<SCRIPT LANGUAGE='JavaScript'>\n");
    s.Append("<!--\n");
    s.Append("function SetInitialFocus()\n");
    s.Append("{\n");
    s.Append(" document.");

    // Find the Form
    System.Web.UI.Control p = control.Parent;
    while (!(p is System.Web.UI.HtmlControls.HtmlForm))
     p = p.Parent;

    s.Append(p.ClientID);
    s.Append("['");
    s.Append(control.UniqueID);
    // Set Focus on the selected item of a RadioButtonList
    System.Web.UI.WebControls.TextBox rbl = control as
System.Web.UI.WebControls.TextBox;
    s.Append("'].focus();\n");
    s.Append(" document.");

    // Find the Form
    p = control.Parent;
    while (!(p is System.Web.UI.HtmlControls.HtmlForm))
     p = p.Parent;

    s.Append(p.ClientID);
    s.Append("['");
    s.Append(control.UniqueID);
    // Set Focus on the selected item of a RadioButtonList
    rbl = control as System.Web.UI.WebControls.TextBox;
    s.Append("'].select();\n");
    s.Append("}\n");

    if (control.Page.SmartNavigation)
     s.Append("window.setTimeout(SetInitialFocus, 500);\n");
    else
     s.Append("window.onload = SetInitialFocus;\n");

    s.Append("// -->\n");
    s.Append("</SCRIPT>");

    // Register Client Script
    control.Page.RegisterClientScriptBlock("InitialFocus", s.ToString());
   }
  }

-- 
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27***
"Tom Egginger" <T.Egginger@ggmmxx.net> wrote in message 
news:258a501c46088$1d968900$a501280a@phx.gbl...
> Hello Matt,
>
> already tried some ClientSide "Action" (javascript -
> focus...)?
>
> Regards
>
> Tom
>
>
>>-----Original Message-----
>>How would I have a textbox on my web form that will
> automatically have a
>>blinking cursor in it when the page loads?
>>Closest I've come so far is having to press tab once to
> put the cursor in
>>there.
>>
>>Thanks!
>>
>>Matt
>>
>>
>>.
>> 

Quantcast