SetFocus

Discussion in 'ASP .Net Mobile' started by José Araujo, Oct 5, 2005.

  1. José Araujo

    José Araujo Guest

    Hi,

    I am trying to set my MobileApp to set the focus to a given component.

    For regular webforms it seems that the classic solution is to use the code
    below, which in turn inserts JavaScript code in the page that does the job.

    I am trying to use the same in my page, but it doesn't work. It doesn't
    insert any code in the resulting HTML page (when you use view source in IE).

    Any ideas? Thanks, José Araujo.

    public static void SetFocus(Control control)
    {
    StringBuilder sb = new StringBuilder();

    sb.Append("\r\n<script language='JavaScript'>\r\n");
    sb.Append("<!--\r\n");
    sb.Append("function SetFocus()\r\n");
    sb.Append("{\r\n");
    sb.Append("\tdocument.");

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

    sb.Append(p.ClientID);
    sb.Append("['");
    sb.Append(control.UniqueID);
    sb.Append("'].focus();\r\n");
    sb.Append("}\r\n");
    sb.Append("window.onload = SetFocus;\r\n");
    sb.Append("// -->\r\n");
    sb.Append("</script>");

    control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
    }
    José Araujo, Oct 5, 2005
    #1
    1. Advertising

  2. José Araujo

    José Araujo Guest

    I resolved this. I wrote my own server control that just renders script
    code.

    "José Araujo" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to set my MobileApp to set the focus to a given component.
    >
    > For regular webforms it seems that the classic solution is to use the code
    > below, which in turn inserts JavaScript code in the page that does the
    > job.
    >
    > I am trying to use the same in my page, but it doesn't work. It doesn't
    > insert any code in the resulting HTML page (when you use view source in
    > IE).
    >
    > Any ideas? Thanks, José Araujo.
    >
    > public static void SetFocus(Control control)
    > {
    > StringBuilder sb = new StringBuilder();
    >
    > sb.Append("\r\n<script language='JavaScript'>\r\n");
    > sb.Append("<!--\r\n");
    > sb.Append("function SetFocus()\r\n");
    > sb.Append("{\r\n");
    > sb.Append("\tdocument.");
    >
    > Control p = control.Parent;
    > while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;
    >
    > sb.Append(p.ClientID);
    > sb.Append("['");
    > sb.Append(control.UniqueID);
    > sb.Append("'].focus();\r\n");
    > sb.Append("}\r\n");
    > sb.Append("window.onload = SetFocus;\r\n");
    > sb.Append("// -->\r\n");
    > sb.Append("</script>");
    >
    > control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
    > }
    >
    >
    José Araujo, Oct 7, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jurjen de Groot

    SetFocus

    Jurjen de Groot, Jun 29, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    611
    dmedina
    Jun 30, 2003
  2. jamess

    asp setfocus and performance

    jamess, Oct 30, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    431
    Saravana [MVP]
    Oct 30, 2003
  3. jaffer the but

    setfocus eh ! help

    jaffer the but, Apr 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    519
    Lars Netzel
    Apr 14, 2004
  4. Mad Scientist Jr

    asp.net setfocus after validation not working

    Mad Scientist Jr, Jul 13, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    6,734
    Mad Scientist Jr
    Jul 15, 2004
  5. JC
    Replies:
    2
    Views:
    9,324
Loading...

Share This Page