Getting the generated name attribute for use in JavaScript

Discussion in 'ASP .Net Building Controls' started by Nathan Sokalski, Nov 18, 2006.

  1. As most of us probably know, ASP.NET controls do not use an exact copy of a
    control's ID for the HTML element's name or id attribute. When generating
    JavaScript as part of a control or event, we need to have a way to reference
    the HTML elements. The only way I have found to reference the HTML elements
    in my generated Javascript is the following:

    "document.getElementById('" & Me.ctrl.ClientID & "')"

    However, this seems like a lot of extra code for each element. Why do the
    controls have a ClientID property but not a ClientName property? If there
    were a ClientName property, wouldn't it make the code a lot less complicated
    and shorten the amount of JavaScript that had to be included with each
    control? Or is this just one of those Microsoft things where they are trying
    to get people to use the id attribute rather than the name attribute?
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Nov 18, 2006
    #1
    1. Advertising

  2. Nathan Sokalski

    Spam Catcher Guest

    "Nathan Sokalski" <> wrote in
    news::

    > Why do the
    > controls have a ClientID property but not a ClientName property? If
    > there were a ClientName property, wouldn't it make the code a lot less
    > complicated and shorten the amount of JavaScript that had to be
    > included with each control? Or is this just one of those Microsoft
    > things where they are trying to get people to use the id attribute
    > rather than the name attribute?
    >


    You can potentially have two objects using the same ClientName - i.e.:

    Form
    +MyLabel
    +MyUserControl
    +MyLabel

    Thus the Form can have a MyLabel and a MyUserControl MyLabel... so if you
    were to refer to MyLabel, which label is it? :)

    But in anycase, you *could* refer to objects by name ... if you know the
    exact layout of the page. If you take a close look at the element IDs,
    they're generated the same each time (provided the control nesting does not
    change).
     
    Spam Catcher, Nov 18, 2006
    #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. ding feng
    Replies:
    2
    Views:
    2,879
    ding feng
    Jun 25, 2003
  2. Nathan Sokalski
    Replies:
    2
    Views:
    399
    Nathan Sokalski
    Nov 19, 2006
  3. Replies:
    2
    Views:
    737
    cwdjrxyz
    Aug 28, 2007
  4. Nathan Sokalski

    Getting the generated name attribute for use in JavaScript

    Nathan Sokalski, Nov 18, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    125
    MikeS
    Nov 18, 2006
  5. Pavils Jurjans
    Replies:
    3
    Views:
    218
    Pavils Jurjans
    Mar 18, 2005
Loading...

Share This Page