Using Javascript in UserControl

Discussion in 'ASP .Net' started by Lau Lei Cheong, May 20, 2004.

  1. Hello,

    I'm writing a usercontrol to be included in my project. The usercontrol
    have a few javascript function to do the client-side tasks. However, the
    controls on the usercontrol seems to change at execution(e,g: the TextBox
    with id & name"txtbox" in usercontrol "folder1" will have name
    "folder1:txtbox" and id "folder1_txtbox" during execution)
    Since I'll use the control in many place inside my project, it's not
    possible for me to hardcode their name. Could anyone suggest some way to
    address the controls?
    The first idea come up in my mind is to use document.getElementById().
    But then I do not know how to gather the ID of the usercontrol itself.
    Any help would be appreciated. Thank you.

    Regards,
    Lau Lei Cheong
    Lau Lei Cheong, May 20, 2004
    #1
    1. Advertising

  2. Lau Lei Cheong

    Darren Clark Guest

    If you want to konw the exact name of the controll that gets sent out in the
    HTML

    use the ClientID propertiy.

    it will return the folder1_txtBox value for you .

    "Lau Lei Cheong" <> wrote in message
    news:%...
    > Hello,
    >
    > I'm writing a usercontrol to be included in my project. The

    usercontrol
    > have a few javascript function to do the client-side tasks. However, the
    > controls on the usercontrol seems to change at execution(e,g: the TextBox
    > with id & name"txtbox" in usercontrol "folder1" will have name
    > "folder1:txtbox" and id "folder1_txtbox" during execution)
    > Since I'll use the control in many place inside my project, it's not
    > possible for me to hardcode their name. Could anyone suggest some way to
    > address the controls?
    > The first idea come up in my mind is to use document.getElementById().
    > But then I do not know how to gather the ID of the usercontrol itself.
    > Any help would be appreciated. Thank you.
    >
    > Regards,
    > Lau Lei Cheong
    >
    >
    Darren Clark, May 20, 2004
    #2
    1. Advertising

  3. You can read up on the subject in this article:

    http://www.aspnetpro.com/features/2004/06/asp200406so_f/asp200406so_f.asp

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net


    "Lau Lei Cheong" <> wrote in message
    news:%...
    > Hello,
    >
    > I'm writing a usercontrol to be included in my project. The

    usercontrol
    > have a few javascript function to do the client-side tasks. However, the
    > controls on the usercontrol seems to change at execution(e,g: the TextBox
    > with id & name"txtbox" in usercontrol "folder1" will have name
    > "folder1:txtbox" and id "folder1_txtbox" during execution)
    > Since I'll use the control in many place inside my project, it's not
    > possible for me to hardcode their name. Could anyone suggest some way to
    > address the controls?
    > The first idea come up in my mind is to use document.getElementById().
    > But then I do not know how to gather the ID of the usercontrol itself.
    > Any help would be appreciated. Thank you.
    >
    > Regards,
    > Lau Lei Cheong
    >
    >
    Steve C. Orr [MVP, MCSD], May 20, 2004
    #3
  4. Oops, I do not have an account to login there.

    Thank you anyway. :)

    "Steve C. Orr [MVP, MCSD]" <> ¦b¶l¥ó
    news:%23$ ¤¤¼¶¼g...
    > You can read up on the subject in this article:
    >
    > http://www.aspnetpro.com/features/2004/06/asp200406so_f/asp200406so_f.asp
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > http://Steve.Orr.net
    >
    >
    > "Lau Lei Cheong" <> wrote in message
    > news:%...
    > > Hello,
    > >
    > > I'm writing a usercontrol to be included in my project. The

    > usercontrol
    > > have a few javascript function to do the client-side tasks. However, the
    > > controls on the usercontrol seems to change at execution(e,g: the

    TextBox
    > > with id & name"txtbox" in usercontrol "folder1" will have name
    > > "folder1:txtbox" and id "folder1_txtbox" during execution)
    > > Since I'll use the control in many place inside my project, it's not
    > > possible for me to hardcode their name. Could anyone suggest some way to
    > > address the controls?
    > > The first idea come up in my mind is to use

    document.getElementById().
    > > But then I do not know how to gather the ID of the usercontrol itself.
    > > Any help would be appreciated. Thank you.
    > >
    > > Regards,
    > > Lau Lei Cheong
    > >
    > >

    >
    >
    Lau Lei Cheong, May 20, 2004
    #4
  5. I'll do more experiments on that. Thank you. :D

    "Darren Clark" <> ¦b¶l¥ó
    news: ¤¤¼¶¼g...
    > If you want to konw the exact name of the controll that gets sent out in

    the
    > HTML
    >
    > use the ClientID propertiy.
    >
    > it will return the folder1_txtBox value for you .
    Lau Lei Cheong, May 20, 2004
    #5
  6. After some experiment, I finally come up with this piece of code.
    Not using ClientID, but it does do the job. :)

    var ctlname = name.substr(0, name.search(/:/i));
    document.getElementById(ctlname + 'mycontrol').value='newvalue';

    I use name property instead of Id to address the control because colon
    is not commonly used in name, while underscore in Id does commonly used
    in Id.

    Regards,
    Lau Lei Cheong

    "Lau Lei Cheong" <> ¦b¶l¥ó
    news: ¤¤¼¶¼g...
    > I'll do more experiments on that. Thank you. :D
    >
    > "Darren Clark" <> ¦b¶l¥ó
    > news: ¤¤¼¶¼g...
    > > If you want to konw the exact name of the controll that gets sent out in

    > the
    > > HTML
    > >
    > > use the ClientID propertiy.
    > >
    > > it will return the folder1_txtBox value for you .

    >
    >
    Lau Lei Cheong, May 21, 2004
    #6
    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. huobazi
    Replies:
    1
    Views:
    682
    David Waz...
    Jul 3, 2003
  2. Rajesh Tiwari

    Can we use a usercontrol inside a usercontrol

    Rajesh Tiwari, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    409
    Rajesh Tiwari
    Aug 12, 2003
  3. nail
    Replies:
    0
    Views:
    1,539
  4. Phl
    Replies:
    2
    Views:
    521
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 18, 2004
  5. Replies:
    2
    Views:
    536
Loading...

Share This Page