setting field focus on an ascx page

Last post 05-09-2008 12:09 PM by uwspstar. 6 replies.

Sort Posts:

  • setting field focus on an ascx page

    05-09-2008, 8:43 AM

    Hi

     I'm trying to set field focus on a text box which is on an ascx page. 

    The ascx page is also a control in an container aspx page.

    The following HTML works for an aspx page in the body section but not for the embedded control:

    Page1.aspx

     onload="document.Page1.textbox1.focus();"

     is there a way to reference the embedded ctrl field to have focus?

     

    Kind Regards BH

  • Re: setting field focus on an ascx page

    05-09-2008, 9:44 AM
    • Loading...
    • Raggers
    • Joined on 02-17-2006, 4:40 PM
    • Germany
    • Posts 122

     simple

    1.On client side sth like this : ascxID_controlID     ( so say id of usercontrol in page is usercontrol1 then  onload="document.getElementById('usercontrol1').textbox1.focus();)

    2.On server side sth like this in page load 

     TextBox t=  usercontrol1.FindControl("TextBox1") as TextBox;
              if (t != null)
                  t.Focus();

    If this solves your problem please mark as answer
  • Re: setting field focus on an ascx page

    05-09-2008, 10:15 AM

    I'm using VB .net

     Not sure if you mean put the code in the html of the aspx page or the ascx page

     

    Kind Regards BH

  • Re: setting field focus on an ascx page

    05-09-2008, 10:51 AM
    • Loading...
    • uwspstar
    • Joined on 03-23-2008, 4:36 PM
    • Milwaukee
    • Posts 138

    Bill Humphrey:

    Hi

     I'm trying to set field focus on a text box which is on an ascx page. 

    The ascx page is also a control in an container aspx page.

    The following HTML works for an aspx page in the body section but not for the embedded control:

    Page1.aspx

     onload="document.Page1.textbox1.focus();"

     is there a way to reference the embedded ctrl field to have focus?

     

    Kind Regards BH

    Step 1:  to open the view source in your IE browser, you will find  the exactly ID which the server assigned to  your  textbox1  ( something like ct1_txtId ...)

    Step 2: in the body (html) add onload (javascript ) function to focus the textbox1 by using the ID

     

    owner of AskBargains.com
    MCAD & MCSD


    If you mark as "Answer"other people can use this answer as a reference
  • Re: setting field focus on an ascx page

    05-09-2008, 10:57 AM
    • Loading...
    • Raggers
    • Joined on 02-17-2006, 4:40 PM
    • Germany
    • Posts 122

    first one is in HTML code(client side)

    servier side code is in aspx page 

     

    If this solves your problem please mark as answer
  • Re: setting field focus on an ascx page

    05-09-2008, 11:10 AM
    Answer
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 6:03 AM
    • Posts 667

    uwspstar:
    Step 1:  to open the view source in your IE browser, you will find  the exactly ID which the server assigned to  your  textbox1  ( something like ct1_txtId ...)
     

    That is bad advice. Wherever possible don't hardcode these values into your code - update your user control name and *wham* your javascript breaks. Instead you could expose some public properties in your user control to return either the client id of your textbox or indeed a reference to the textbox itself, e.g. (in code-behind of your user control):

        // Return the clientID of my textbox
        public string TextBox1ClientID
        {
            get { return TextBox1.ClientID; }
        }

        // Return a reference to my textbox
        public TextBox TextBox1Ref
        {
            get { return TextBox1; }
        }

    In the javascript of your page then you could get the client id and set focus in javascript:

    window.onload = function(){
        var ele = document.getElementById("<%=TestControl1.TextBox1ClientID%>");         // Use this if your public property returns just the client id of your textbox
        var ele = document.getElementById("<%=TestControl1.TextBox1Ref.ClientID%>");   // Use this if your public property returns a reference to your textbox
        ele.focus();
    }

    Alternatively if you wanted to set focus in the code-behind of your page you could create a public method in your user control which would do it for you:

    -- User Control code-behind --
    public void TextBox1SetFocus()
    {
       TextBox1.Focus();
    }

    -- Page code-behind (Page_Load or wherever appropriate) --
    MyUserControl.TextBox1SetFocus()
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: setting field focus on an ascx page

    05-09-2008, 12:09 PM
    • Loading...
    • uwspstar
    • Joined on 03-23-2008, 4:36 PM
    • Milwaukee
    • Posts 138

    ramblor:

    uwspstar:
    Step 1:  to open the view source in your IE browser, you will find  the exactly ID which the server assigned to  your  textbox1  ( something like ct1_txtId ...)
     

    That is bad advice. Wherever possible don't hardcode these values into your code - update your user control name and *wham* your javascript breaks. Instead you could expose some public properties in your user control to return either the client id of your textbox or indeed a reference to the textbox itself, e.g. (in code-behind of your user control):

        // Return the clientID of my textbox
        public string TextBox1ClientID
        {
            get { return TextBox1.ClientID; }
        }


        // Return a reference to my textbox
        public TextBox TextBox1Ref
        {
            get { return TextBox1; }
        }

    In the javascript of your page then you could get the client id and set focus in javascript:

    window.onload = function(){
        var ele = document.getElementById("<%=TestControl1.TextBox1ClientID%>");         // Use this if your public property returns just the client id of your textbox
        var ele = document.getElementById("<%=TestControl1.TextBox1Ref.ClientID%>");   // Use this if your public property returns a reference to your textbox
        ele.focus();
    }

    Alternatively if you wanted to set focus in the code-behind of your page you could create a public method in your user control which would do it for you:

    -- User Control code-behind --
    public void TextBox1SetFocus()
    {
       TextBox1.Focus();
    }

    -- Page code-behind (Page_Load or wherever appropriate) --
    MyUserControl.TextBox1SetFocus()

    I agree !!! avoid the hard code as possible as you can

    owner of AskBargains.com
    MCAD & MCSD


    If you mark as "Answer"other people can use this answer as a reference
Page 1 of 1 (7 items)