master-pages: naming problem for client-side scripting

Discussion in 'ASP .Net Web Controls' started by Roberto Kohler, Dec 19, 2005.

  1. ASP.NET renames Textboxes and other controls in aspx files that use
    master-pages ?

    I've discovered that aspx files that use master-pages have their Textboxes
    and other controls renamed before they are rendered. For example I have an
    aspx file with a textbox named "City", when it gets loaded at the client,
    the textbox "txtCity" is now named "ctl00$bodyContentPlaceHolder$City".

    That means that if I am writing client side scripts that need to manipulate
    the Textbox "City", they need to be re-written to manipulate
    "ctl00$bodyContentPlaceHolder$City" instead of "City".

    Is there a way to dynamically determine the text string ASP.NET is going to
    prepend to the html tag names?
    Is the text string ASP.NET prepends always "ctl00$bodyContentPlaceHolder$" ?
    What is the best way to handle this?
    Roberto Kohler, Dec 19, 2005
    #1
    1. Advertising

  2. Hi Roberto,

    I usually employ a JavaScript function that looks for a control of a
    particular tagname that ends with the ID that I want, e.g. the following
    function would get you one control at a time of type input (which is the
    object rendered by a textbox):

    function findControl(ControlID)
    { var ret=null;
    var aControls = document.getElementsByTagName("input");
    if (aControls)
    { for (var i=0; i< aControls.length ; i++)
    {
    if (aControls.id.lastIndexOf(ControlID) == aControls.id.length -
    ControlID.length &&
    aControls.id.length != ControlID.length &&
    aControls.id.lastIndexOf(ControlID) > 0 )
    {
    ret =aControls;
    break;
    }
    }
    }
    return ret;
    }

    You can use it by writing
    var txtCity = findControl("City");
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "Roberto Kohler" wrote:

    > ASP.NET renames Textboxes and other controls in aspx files that use
    > master-pages ?
    >
    > I've discovered that aspx files that use master-pages have their Textboxes
    > and other controls renamed before they are rendered. For example I have an
    > aspx file with a textbox named "City", when it gets loaded at the client,
    > the textbox "txtCity" is now named "ctl00$bodyContentPlaceHolder$City".
    >
    > That means that if I am writing client side scripts that need to manipulate
    > the Textbox "City", they need to be re-written to manipulate
    > "ctl00$bodyContentPlaceHolder$City" instead of "City".
    >
    > Is there a way to dynamically determine the text string ASP.NET is going to
    > prepend to the html tag names?
    > Is the text string ASP.NET prepends always "ctl00$bodyContentPlaceHolder$" ?
    > What is the best way to handle this?
    >
    >
    >
    Phillip Williams, Dec 19, 2005
    #2
    1. Advertising

  3. Phillip,

    That worked!

    Thanks.
    Roberto Kohler, Dec 21, 2005
    #3
  4. Phillip Williams, Dec 21, 2005
    #4
    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. Loui Mercieca

    Focus problem and client Side scripting

    Loui Mercieca, Aug 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    433
    Loui Mercieca
    Aug 16, 2005
  2. Alan Silver
    Replies:
    1
    Views:
    4,298
    Alan Silver
    Dec 1, 2005
  3. Doug McCann
    Replies:
    1
    Views:
    993
    William Brogden
    Aug 5, 2004
  4. Replies:
    1
    Views:
    3,191
    PeterKellner
    May 16, 2006
  5. Kathryn
    Replies:
    9
    Views:
    189
    William Morris
    Oct 17, 2003
Loading...

Share This Page