Referencing subcontrols within UserControl

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Referencing subcontrols within UserControl

    Does anyone know how to reference a subcontrol of a UserControl within
    client-side script. For example, if I have a TextBox as one of the elements
    of my UserControl with an id of "MyTextBox", how do I reference it within
    some jscript function. If I try to just reference it as MyTextBox I get an
    error saying that MyTextBox is undefined.

    From looking at the generated source, I believe that there are two issues
    involved. One is that the subcontrol's ID is getting renamed to something
    like Foo1_MyTextBox, where Foo is the name of the UserControl. The second
    issue is that the reference seems to need to be qualified by it's parent
    form's ID, e.g. Form1.Foo1_MyTextBox. If I reference the TextBox that way
    it works, but there are a couple of problems, First, I can't be sure what
    the renaming be. I assume that if I have more than one instance of the
    UserControl on a form that the prefix will change. The second problem is
    that I don't know what the ID if the containing form is going to be so I
    can't know how to qualify the TextBox's name.

    Thanks in advance.

    -Mark


    Mark Friedman Guest

  2. Similar Questions and Discussions

    1. referencing and anchor from within a usercontrol (ascx)
      Hi, I am trying to reference an anchor in a user control with a url. This worked in 1.1 but no longer works in 2.0. The ascx control is...
    2. Question about DataBindings in subcontrols
      Hi. I'm developing a custom webcontrol that have a property derived from BaseCollection that realizes a collection of particular web controls. When...
    3. VS.NET not creating the Site for SubControls
      I'm experiencing the same problem as was discussed in a thread with this subject "VS.NET not creating the Site for SubControls" I have a control...
    4. Referencing a panel on a webform from an usercontrol
      My question is whether it's possible to change the properties of a panel, that resides on the webform containing the usercontrol, in the...
    5. Use LoadControl to load a usercontrol but the webcontrol in the usercontrol can not AutoPostBack
      a uscontrol test.ascx have a dropdownlist web control the dropdownlist's AutoPostBack property is set "true" but when i use...
  3. #2

    Default Re: Referencing subcontrols within UserControl

    Use the ClientID property to get the name of the control in the resulting
    HTML.

    --
    James J. Foster, DotNetCoders
    [url]http://www.dotnetcoders.com[/url]


    "Mark Friedman" <bingster@yahoo.com> wrote in message
    news:%239p0nijRDHA.2676@TK2MSFTNGP10.phx.gbl...
    > Does anyone know how to reference a subcontrol of a UserControl within
    > client-side script. For example, if I have a TextBox as one of the
    elements
    > of my UserControl with an id of "MyTextBox", how do I reference it within
    > some jscript function. If I try to just reference it as MyTextBox I get
    an
    > error saying that MyTextBox is undefined.
    >
    > From looking at the generated source, I believe that there are two issues
    > involved. One is that the subcontrol's ID is getting renamed to something
    > like Foo1_MyTextBox, where Foo is the name of the UserControl. The second
    > issue is that the reference seems to need to be qualified by it's parent
    > form's ID, e.g. Form1.Foo1_MyTextBox. If I reference the TextBox that way
    > it works, but there are a couple of problems, First, I can't be sure what
    > the renaming be. I assume that if I have more than one instance of the
    > UserControl on a form that the prefix will change. The second problem is
    > that I don't know what the ID if the containing form is going to be so I
    > can't know how to qualify the TextBox's name.
    >
    > Thanks in advance.
    >
    > -Mark
    >
    >

    James J. Foster Guest

  4. #3

    Default Re: Referencing subcontrols within UserControl

    I take your response to refer to client-side script that would be generated
    by my UserControl, but what I'd really like to do is just write the script
    code directly in the page. Is there a way to do what I want without
    generating the script in the control?

    -Mark

    "James J. Foster" <james@dotnetcoders.com> wrote in message
    news:ucvrgxjRDHA.3796@tk2msftngp13.phx.gbl...
    > Use the ClientID property to get the name of the control in the resulting
    > HTML.
    >
    > --
    > James J. Foster, DotNetCoders
    > [url]http://www.dotnetcoders.com[/url]
    >
    >
    > "Mark Friedman" <bingster@yahoo.com> wrote in message
    > news:%239p0nijRDHA.2676@TK2MSFTNGP10.phx.gbl...
    > > Does anyone know how to reference a subcontrol of a UserControl within
    > > client-side script. For example, if I have a TextBox as one of the
    > elements
    > > of my UserControl with an id of "MyTextBox", how do I reference it
    within
    > > some jscript function. If I try to just reference it as MyTextBox I get
    > an
    > > error saying that MyTextBox is undefined.
    > >
    > > From looking at the generated source, I believe that there are two
    issues
    > > involved. One is that the subcontrol's ID is getting renamed to
    something
    > > like Foo1_MyTextBox, where Foo is the name of the UserControl. The
    second
    > > issue is that the reference seems to need to be qualified by it's parent
    > > form's ID, e.g. Form1.Foo1_MyTextBox. If I reference the TextBox that
    way
    > > it works, but there are a couple of problems, First, I can't be sure
    what
    > > the renaming be. I assume that if I have more than one instance of the
    > > UserControl on a form that the prefix will change. The second problem
    is
    > > that I don't know what the ID if the containing form is going to be so I
    > > can't know how to qualify the TextBox's name.
    > >
    > > Thanks in advance.
    > >
    > > -Mark
    > >
    > >
    >
    >

    Mark Friedman Guest

  5. #4

    Default Re: Referencing subcontrols within UserControl


    <script>
    var ctl = document.getElementById('<% =
    FindControl("myusercontrol").FindControl("mysubcon trol").ClientId %>');
    </script>

    "Mark Friedman" <bingster@yahoo.com> wrote in message
    news:uyS7$ikRDHA.632@TK2MSFTNGP12.phx.gbl...
    > I take your response to refer to client-side script that would be
    generated
    > by my UserControl, but what I'd really like to do is just write the script
    > code directly in the page. Is there a way to do what I want without
    > generating the script in the control?
    >
    > -Mark
    >
    > "James J. Foster" <james@dotnetcoders.com> wrote in message
    > news:ucvrgxjRDHA.3796@tk2msftngp13.phx.gbl...
    > > Use the ClientID property to get the name of the control in the
    resulting
    > > HTML.
    > >
    > > --
    > > James J. Foster, DotNetCoders
    > > [url]http://www.dotnetcoders.com[/url]
    > >
    > >
    > > "Mark Friedman" <bingster@yahoo.com> wrote in message
    > > news:%239p0nijRDHA.2676@TK2MSFTNGP10.phx.gbl...
    > > > Does anyone know how to reference a subcontrol of a UserControl within
    > > > client-side script. For example, if I have a TextBox as one of the
    > > elements
    > > > of my UserControl with an id of "MyTextBox", how do I reference it
    > within
    > > > some jscript function. If I try to just reference it as MyTextBox I
    get
    > > an
    > > > error saying that MyTextBox is undefined.
    > > >
    > > > From looking at the generated source, I believe that there are two
    > issues
    > > > involved. One is that the subcontrol's ID is getting renamed to
    > something
    > > > like Foo1_MyTextBox, where Foo is the name of the UserControl. The
    > second
    > > > issue is that the reference seems to need to be qualified by it's
    parent
    > > > form's ID, e.g. Form1.Foo1_MyTextBox. If I reference the TextBox that
    > way
    > > > it works, but there are a couple of problems, First, I can't be sure
    > what
    > > > the renaming be. I assume that if I have more than one instance of the
    > > > UserControl on a form that the prefix will change. The second problem
    > is
    > > > that I don't know what the ID if the containing form is going to be so
    I
    > > > can't know how to qualify the TextBox's name.
    > > >
    > > > Thanks in advance.
    > > >
    > > > -Mark
    > > >
    > > >
    > >
    > >
    >
    >

    bruce barker Guest

  6. #5

    Default Re: Referencing subcontrols within UserControl

    Thank you very much.

    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Christopher Young Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139