Dynamically created user controls

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

  1. #1

    Default Dynamically created user controls

    In ASP.Net, I am working with some in-house software that dynamically creates
    a form based on rows in a database table. For example, most pages consist of
    a header control, center control, and a footer control. By adding rows to
    specific tables in the database, the form can be changed to add a breadcrumb
    control or a calendar control.

    I have discovered that for each new control that is added, ASP.Net adds a
    _ctl# prefix. In javascript, when I need to reference a control, I can use
    _ctl1_FirstName. Today my client side code stopped working because someone
    added a calendar control to the form and now my textbox is _ctl2_FirstName.

    Can anyone think of a way in Javascript to figure out what the prefix is for
    a given control? Would the best approach be to loop through all the controls
    to find the ones with a name I'm looking for? If so, does anyone have a
    quick code sample?

    It wouldn't be a problem if there were only one or two forms, but there are
    many.

    Thanks,
    Denise
    Denise Guest

  2. Similar Questions and Discussions

    1. Eventhandling from dynamically created controls
      I have some questions reguarding event handling of dynamically added controls. An example scenario (se code below): I have one button declared...
    2. Access dynamically created controls
      Hi there, I read a lot about this issue but still got no clear answer that solves my problem. I've a Web User Control with a placeholder called...
    3. Retrieving Values from dynamically created controls in a user control
      Hi, Dynamic control should be load every time page load, even on post back. take out control creation from IsPostBack condition. Natty Gur ...
    4. Referencing Dynamically created controls
      Can anyone tell me how to reference a a control, e.g. a control I have added to the controls collection without specifically naming it. Also can...
    5. get formvalues - dynamically created controls
      C.H., You could loop through them using their index. Instead of using FindControl and the control's name you could just use the index of the...
  3. #2

    Default Re: Dynamically created user controls

    If you declare the tag yourself use the ID="_myID". Unfortunately if this
    control is nested under other controls then it's possible this ID isn't unique,
    so you can also use Control.ClientID to know the proper ID to use from your
    javascript.

    -Brock
    DevelopMentor
    [url]http://staff.develop.com/ballen[/url]


    > In ASP.Net, I am working with some in-house software that dynamically
    > creates a form based on rows in a database table. For example, most
    > pages consist of a header control, center control, and a footer
    > control. By adding rows to specific tables in the database, the form
    > can be changed to add a breadcrumb control or a calendar control.
    >
    > I have discovered that for each new control that is added, ASP.Net
    > adds a _ctl# prefix. In javascript, when I need to reference a
    > control, I can use _ctl1_FirstName. Today my client side code stopped
    > working because someone added a calendar control to the form and now
    > my textbox is _ctl2_FirstName.
    >
    > Can anyone think of a way in Javascript to figure out what the prefix
    > is for a given control? Would the best approach be to loop through
    > all the controls to find the ones with a name I'm looking for? If so,
    > does anyone have a quick code sample?
    >
    > It wouldn't be a problem if there were only one or two forms, but
    > there are many.
    >
    > Thanks,
    > Denise


    Brock Allen Guest

  4. #3

    Default Re: Dynamically created user controls

    Thanks for your response. I don't understand how to declare the tag myself.
    If I name the control _ctl9_UserName, it gets generated into
    _ctl2__ctl9_UserName.

    And I believe the control.clientid is on the server side. I need to be able
    to find the control on the client side, withouth supposing the _ctl# prefix
    that ASP.Net will generate.

    Denise


    "Brock Allen" wrote:
    > If you declare the tag yourself use the ID="_myID". Unfortunately if this
    > control is nested under other controls then it's possible this ID isn't unique,
    > so you can also use Control.ClientID to know the proper ID to use from your
    > javascript.
    >
    > -Brock
    > DevelopMentor
    > [url]http://staff.develop.com/ballen[/url]
    >
    >
    >
    > > In ASP.Net, I am working with some in-house software that dynamically
    > > creates a form based on rows in a database table. For example, most
    > > pages consist of a header control, center control, and a footer
    > > control. By adding rows to specific tables in the database, the form
    > > can be changed to add a breadcrumb control or a calendar control.
    > >
    > > I have discovered that for each new control that is added, ASP.Net
    > > adds a _ctl# prefix. In javascript, when I need to reference a
    > > control, I can use _ctl1_FirstName. Today my client side code stopped
    > > working because someone added a calendar control to the form and now
    > > my textbox is _ctl2_FirstName.
    > >
    > > Can anyone think of a way in Javascript to figure out what the prefix
    > > is for a given control? Would the best approach be to loop through
    > > all the controls to find the ones with a name I'm looking for? If so,
    > > does anyone have a quick code sample?
    > >
    > > It wouldn't be a problem if there were only one or two forms, but
    > > there are many.
    > >
    > > Thanks,
    > > Denise
    >
    >
    >
    >
    Denise Guest

  5. #4

    Default Re: Dynamically created user controls

    I don't understand what you mean by declaring the tag myself. If I name my
    control _ctl9_UserName, is will be generated as _ctl2___ctl9_UserName.

    And isn't the Control.ClientId on the server side? Is there any way to do
    this client side?

    Denise


    Denise Guest

  6. #5

    Default Re: Dynamically created user controls

    Yes, Control.ClientID is server side. You need to get this value on the server
    and emit it into the javascript that's being sent back to the client. This
    could be done as easily as a parameter to a function. Something like this
    (quickly written pseudo-code, so may have syntax errors):

    <script language="javascript">
    function Work(element)
    {
    document.getElementByID(element).innerHTML = "something different";
    }
    </script>

    <asp:Label runat=server id=_myLabel Text="something" />

    <script runat=server>
    void Page_Load(...)
    {
    _myLabel.Attributes.Add("onclick", String.Format("Work({0})", _myLabel.ClientID));
    }
    </script>

    -Brock
    DevelopMentor
    [url]http://staff.develop.com/ballen[/url]


    > Thanks for your response. I don't understand how to declare the tag
    > myself.
    > If I name the control _ctl9_UserName, it gets generated into
    > _ctl2__ctl9_UserName.
    > And I believe the control.clientid is on the server side. I need to
    > be able to find the control on the client side, withouth supposing the
    > _ctl# prefix that ASP.Net will generate.
    >
    > Denise
    >
    > "Brock Allen" wrote:
    >
    >> If you declare the tag yourself use the ID="_myID". Unfortunately if
    >> this control is nested under other controls then it's possible this
    >> ID isn't unique, so you can also use Control.ClientID to know the
    >> proper ID to use from your javascript.
    >>
    >> -Brock
    >> DevelopMentor
    >> [url]http://staff.develop.com/ballen[/url]
    >>> In ASP.Net, I am working with some in-house software that
    >>> dynamically creates a form based on rows in a database table. For
    >>> example, most pages consist of a header control, center control, and
    >>> a footer control. By adding rows to specific tables in the
    >>> database, the form can be changed to add a breadcrumb control or a
    >>> calendar control.
    >>>
    >>> I have discovered that for each new control that is added, ASP.Net
    >>> adds a _ctl# prefix. In javascript, when I need to reference a
    >>> control, I can use _ctl1_FirstName. Today my client side code
    >>> stopped working because someone added a calendar control to the form
    >>> and now my textbox is _ctl2_FirstName.
    >>>
    >>> Can anyone think of a way in Javascript to figure out what the
    >>> prefix is for a given control? Would the best approach be to loop
    >>> through all the controls to find the ones with a name I'm looking
    >>> for? If so, does anyone have a quick code sample?
    >>>
    >>> It wouldn't be a problem if there were only one or two forms, but
    >>> there are many.
    >>>
    >>> Thanks,
    >>> Denise


    Brock Allen 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