Problem adding multiple instances of a custom control to aspx page

Discussion in 'ASP .Net Web Controls' started by Owen Evans, Jun 7, 2004.

  1. Owen Evans

    Owen Evans Guest

    I've a web page that adds a custom control every time a button is clicked.
    I'm keeping a reference to each added control in Session state so I can
    recreate existing controls every postback. However, the controls are always
    added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get an error:
    "Multiple controls with the same ID '_ctl0' were found. FindControl requires
    that controls have unique IDs". What do I need to do to get this to work?

    Thanks,

    Owen
    Owen Evans, Jun 7, 2004
    #1
    1. Advertising

  2. You wrote this custom server control? What class did you inherit from?

    --
    RobertK
    { Clever? No just smart. }

    "Owen Evans" <> wrote in message
    news:...
    > I've a web page that adds a custom control every time a button is clicked.
    > I'm keeping a reference to each added control in Session state so I can
    > recreate existing controls every postback. However, the controls are

    always
    > added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get an error:
    > "Multiple controls with the same ID '_ctl0' were found. FindControl

    requires
    > that controls have unique IDs". What do I need to do to get this to work?
    >
    > Thanks,
    >
    > Owen
    >
    >
    Robert Koritnik, Jun 7, 2004
    #2
    1. Advertising

  3. Owen Evans

    Owen Evans Guest

    I'm basically doing a proof of concept for myself, before I dive into
    development proper. So my test control inherits from Button, with a counter
    to show how many times it's been clicked. But anything that inherits from
    Control has the same problem (whether or not I also implement
    INamingContainer).


    "Robert Koritnik" <> wrote in message
    news:...
    > You wrote this custom server control? What class did you inherit from?
    >
    > --
    > RobertK
    > { Clever? No just smart. }
    >
    > "Owen Evans" <> wrote in message
    > news:...
    > > I've a web page that adds a custom control every time a button is

    clicked.
    > > I'm keeping a reference to each added control in Session state so I can
    > > recreate existing controls every postback. However, the controls are

    > always
    > > added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get an

    error:
    > > "Multiple controls with the same ID '_ctl0' were found. FindControl

    > requires
    > > that controls have unique IDs". What do I need to do to get this to

    work?
    > >
    > > Thanks,
    > >
    > > Owen
    > >
    > >

    >
    >
    Owen Evans, Jun 7, 2004
    #3
  4. Owen Evans

    cld Guest

    [This followup was posted to
    microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was sent
    to the cited author.]

    In article <>,
    says...
    > I've a web page that adds a custom control every time a button is clicked.
    > I'm keeping a reference to each added control in Session state so I can
    > recreate existing controls every postback. However, the controls are always
    > added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get an error:
    > "Multiple controls with the same ID '_ctl0' were found. FindControl requires
    > that controls have unique IDs". What do I need to do to get this to work?
    >
    > Thanks,
    >
    > Owen

    Perhaps you are missing a INamingContainer implementation. Please let me
    know if this helps you
    cld, Jun 7, 2004
    #4
  5. Owen Evans

    Owen Evans Guest

    Re: Problem adding multiple instances of a custom control to aspx page (solved by hackery)

    INamingContainer is irrelevant - same error whether or not it's declared.
    This is a problem with the control itself and its relationship to the
    containing page, not the IDs of child controls.

    The problem was actually that when the controls are created, they're always
    created with an ID of _ctl0. So, when I pull a reference to the control out
    of session state and add it back to the page, I end up adding multiple
    controls with the same ID. This confiuses ASP.NET, and so I end up with an
    error.

    I didn't find a way to persuade ASP.NET to automatically create a unique ID,
    so I'm now generating a Guid to use as a Session state key and control ID,
    and explicitly assigning my controls an ID in the Init phase of the page
    lifecycle.

    I'm not sure that a Guid is the best/most elegant way to do it, but it's the
    least amount of code required to generate something I know to be unique
    (hence the note "solved by hackery")..

    "cld" <> wrote in message
    news:...
    > [This followup was posted to
    > microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was sent
    > to the cited author.]
    >
    > In article <>,
    > says...
    > > I've a web page that adds a custom control every time a button is

    clicked.
    > > I'm keeping a reference to each added control in Session state so I can
    > > recreate existing controls every postback. However, the controls are

    always
    > > added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get an

    error:
    > > "Multiple controls with the same ID '_ctl0' were found. FindControl

    requires
    > > that controls have unique IDs". What do I need to do to get this to

    work?
    > >
    > > Thanks,
    > >
    > > Owen

    > Perhaps you are missing a INamingContainer implementation. Please let me
    > know if this helps you
    Owen Evans, Jun 8, 2004
    #5
  6. Re: Problem adding multiple instances of a custom control to aspx page (solved by hackery)

    why are you adding your control to cache?
    controls don't work right when serialized./deserialized from cache (at least
    in 1.0 version) not sure if they addressed that for 1.1

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "Owen Evans" <> wrote in message
    news:%...
    > INamingContainer is irrelevant - same error whether or not it's declared.
    > This is a problem with the control itself and its relationship to the
    > containing page, not the IDs of child controls.
    >
    > The problem was actually that when the controls are created, they're
    > always
    > created with an ID of _ctl0. So, when I pull a reference to the control
    > out
    > of session state and add it back to the page, I end up adding multiple
    > controls with the same ID. This confiuses ASP.NET, and so I end up with an
    > error.
    >
    > I didn't find a way to persuade ASP.NET to automatically create a unique
    > ID,
    > so I'm now generating a Guid to use as a Session state key and control ID,
    > and explicitly assigning my controls an ID in the Init phase of the page
    > lifecycle.
    >
    > I'm not sure that a Guid is the best/most elegant way to do it, but it's
    > the
    > least amount of code required to generate something I know to be unique
    > (hence the note "solved by hackery")..
    >
    > "cld" <> wrote in message
    > news:...
    >> [This followup was posted to
    >> microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was sent
    >> to the cited author.]
    >>
    >> In article <>,
    >> says...
    >> > I've a web page that adds a custom control every time a button is

    > clicked.
    >> > I'm keeping a reference to each added control in Session state so I can
    >> > recreate existing controls every postback. However, the controls are

    > always
    >> > added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get an

    > error:
    >> > "Multiple controls with the same ID '_ctl0' were found. FindControl

    > requires
    >> > that controls have unique IDs". What do I need to do to get this to

    > work?
    >> >
    >> > Thanks,
    >> >
    >> > Owen

    >> Perhaps you are missing a INamingContainer implementation. Please let me
    >> know if this helps you

    >
    >
    Alvin Bruney [MVP], Jun 8, 2004
    #6
  7. Owen Evans

    Owen Evans Guest

    Re: Problem adding multiple instances of a custom control to aspx page (solved by hackery)

    The control I'm making is basically an editable datagrid. I'm serializing
    multiple instances of it to Session (Session != Cache, right?) so that I can
    re-add them to the page after a postback.
    What other method would you suggest?


    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    news:...
    > why are you adding your control to cache?
    > controls don't work right when serialized./deserialized from cache (at

    least
    > in 1.0 version) not sure if they addressed that for 1.1
    >
    > --
    > Regards,
    > Alvin Bruney
    > [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    > Got tidbits? Get it here... http://tinyurl.com/27cok
    > "Owen Evans" <> wrote in message
    > news:%...
    > > INamingContainer is irrelevant - same error whether or not it's

    declared.
    > > This is a problem with the control itself and its relationship to the
    > > containing page, not the IDs of child controls.
    > >
    > > The problem was actually that when the controls are created, they're
    > > always
    > > created with an ID of _ctl0. So, when I pull a reference to the control
    > > out
    > > of session state and add it back to the page, I end up adding multiple
    > > controls with the same ID. This confiuses ASP.NET, and so I end up with

    an
    > > error.
    > >
    > > I didn't find a way to persuade ASP.NET to automatically create a unique
    > > ID,
    > > so I'm now generating a Guid to use as a Session state key and control

    ID,
    > > and explicitly assigning my controls an ID in the Init phase of the page
    > > lifecycle.
    > >
    > > I'm not sure that a Guid is the best/most elegant way to do it, but it's
    > > the
    > > least amount of code required to generate something I know to be unique
    > > (hence the note "solved by hackery")..
    > >
    > > "cld" <> wrote in message
    > > news:...
    > >> [This followup was posted to
    > >> microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was

    sent
    > >> to the cited author.]
    > >>
    > >> In article <>,
    > >> says...
    > >> > I've a web page that adds a custom control every time a button is

    > > clicked.
    > >> > I'm keeping a reference to each added control in Session state so I

    can
    > >> > recreate existing controls every postback. However, the controls are

    > > always
    > >> > added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get an

    > > error:
    > >> > "Multiple controls with the same ID '_ctl0' were found. FindControl

    > > requires
    > >> > that controls have unique IDs". What do I need to do to get this to

    > > work?
    > >> >
    > >> > Thanks,
    > >> >
    > >> > Owen
    > >> Perhaps you are missing a INamingContainer implementation. Please let

    me
    > >> know if this helps you

    > >
    > >

    >
    >
    Owen Evans, Jun 10, 2004
    #7
  8. Re: Problem adding multiple instances of a custom control to aspx page (solved by hackery)

    no this is not the right approach. but it is way too late to do it the right
    way.
    and besides, if it works then carry on

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "Owen Evans" <> wrote in message
    news:...
    > The control I'm making is basically an editable datagrid. I'm serializing
    > multiple instances of it to Session (Session != Cache, right?) so that I
    > can
    > re-add them to the page after a postback.
    > What other method would you suggest?
    >
    >
    > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > news:...
    >> why are you adding your control to cache?
    >> controls don't work right when serialized./deserialized from cache (at

    > least
    >> in 1.0 version) not sure if they addressed that for 1.1
    >>
    >> --
    >> Regards,
    >> Alvin Bruney
    >> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    >> Got tidbits? Get it here... http://tinyurl.com/27cok
    >> "Owen Evans" <> wrote in message
    >> news:%...
    >> > INamingContainer is irrelevant - same error whether or not it's

    > declared.
    >> > This is a problem with the control itself and its relationship to the
    >> > containing page, not the IDs of child controls.
    >> >
    >> > The problem was actually that when the controls are created, they're
    >> > always
    >> > created with an ID of _ctl0. So, when I pull a reference to the control
    >> > out
    >> > of session state and add it back to the page, I end up adding multiple
    >> > controls with the same ID. This confiuses ASP.NET, and so I end up with

    > an
    >> > error.
    >> >
    >> > I didn't find a way to persuade ASP.NET to automatically create a
    >> > unique
    >> > ID,
    >> > so I'm now generating a Guid to use as a Session state key and control

    > ID,
    >> > and explicitly assigning my controls an ID in the Init phase of the
    >> > page
    >> > lifecycle.
    >> >
    >> > I'm not sure that a Guid is the best/most elegant way to do it, but
    >> > it's
    >> > the
    >> > least amount of code required to generate something I know to be unique
    >> > (hence the note "solved by hackery")..
    >> >
    >> > "cld" <> wrote in message
    >> > news:...
    >> >> [This followup was posted to
    >> >> microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was

    > sent
    >> >> to the cited author.]
    >> >>
    >> >> In article <>,
    >> >> says...
    >> >> > I've a web page that adds a custom control every time a button is
    >> > clicked.
    >> >> > I'm keeping a reference to each added control in Session state so I

    > can
    >> >> > recreate existing controls every postback. However, the controls are
    >> > always
    >> >> > added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get an
    >> > error:
    >> >> > "Multiple controls with the same ID '_ctl0' were found. FindControl
    >> > requires
    >> >> > that controls have unique IDs". What do I need to do to get this to
    >> > work?
    >> >> >
    >> >> > Thanks,
    >> >> >
    >> >> > Owen
    >> >> Perhaps you are missing a INamingContainer implementation. Please let

    > me
    >> >> know if this helps you
    >> >
    >> >

    >>
    >>

    >
    >
    Alvin Bruney [MVP], Jun 10, 2004
    #8
  9. Owen Evans

    Owen Evans Guest

    Re: Problem adding multiple instances of a custom control to aspx page (solved by hackery)

    This isn't a production app; I can happlly rewrite it for as long as I want.
    For the record, what *is* the right way?

    Owen

    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    news:...
    > no this is not the right approach. but it is way too late to do it the

    right
    > way.
    > and besides, if it works then carry on
    >
    > --
    > Regards,
    > Alvin Bruney
    > [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    > Got tidbits? Get it here... http://tinyurl.com/27cok
    > "Owen Evans" <> wrote in message
    > news:...
    > > The control I'm making is basically an editable datagrid. I'm

    serializing
    > > multiple instances of it to Session (Session != Cache, right?) so that I
    > > can
    > > re-add them to the page after a postback.
    > > What other method would you suggest?
    > >
    > >
    > > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > > news:...
    > >> why are you adding your control to cache?
    > >> controls don't work right when serialized./deserialized from cache (at

    > > least
    > >> in 1.0 version) not sure if they addressed that for 1.1
    > >>
    > >> --
    > >> Regards,
    > >> Alvin Bruney
    > >> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    > >> Got tidbits? Get it here... http://tinyurl.com/27cok
    > >> "Owen Evans" <> wrote in message
    > >> news:%...
    > >> > INamingContainer is irrelevant - same error whether or not it's

    > > declared.
    > >> > This is a problem with the control itself and its relationship to the
    > >> > containing page, not the IDs of child controls.
    > >> >
    > >> > The problem was actually that when the controls are created, they're
    > >> > always
    > >> > created with an ID of _ctl0. So, when I pull a reference to the

    control
    > >> > out
    > >> > of session state and add it back to the page, I end up adding

    multiple
    > >> > controls with the same ID. This confiuses ASP.NET, and so I end up

    with
    > > an
    > >> > error.
    > >> >
    > >> > I didn't find a way to persuade ASP.NET to automatically create a
    > >> > unique
    > >> > ID,
    > >> > so I'm now generating a Guid to use as a Session state key and

    control
    > > ID,
    > >> > and explicitly assigning my controls an ID in the Init phase of the
    > >> > page
    > >> > lifecycle.
    > >> >
    > >> > I'm not sure that a Guid is the best/most elegant way to do it, but
    > >> > it's
    > >> > the
    > >> > least amount of code required to generate something I know to be

    unique
    > >> > (hence the note "solved by hackery")..
    > >> >
    > >> > "cld" <> wrote in message
    > >> > news:...
    > >> >> [This followup was posted to
    > >> >> microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was

    > > sent
    > >> >> to the cited author.]
    > >> >>
    > >> >> In article <>,
    > >> >> says...
    > >> >> > I've a web page that adds a custom control every time a button is
    > >> > clicked.
    > >> >> > I'm keeping a reference to each added control in Session state so

    I
    > > can
    > >> >> > recreate existing controls every postback. However, the controls

    are
    > >> > always
    > >> >> > added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get

    an
    > >> > error:
    > >> >> > "Multiple controls with the same ID '_ctl0' were found.

    FindControl
    > >> > requires
    > >> >> > that controls have unique IDs". What do I need to do to get this

    to
    > >> > work?
    > >> >> >
    > >> >> > Thanks,
    > >> >> >
    > >> >> > Owen
    > >> >> Perhaps you are missing a INamingContainer implementation. Please

    let
    > > me
    > >> >> know if this helps you
    > >> >
    > >> >
    > >>
    > >>

    > >
    > >

    >
    >
    Owen Evans, Jun 10, 2004
    #9
  10. Re: Problem adding multiple instances of a custom control to aspx page (solved by hackery)

    well you should actually be serializing the data in the control, or caching
    the data. The control is just a shell which can be created at will. Then you
    can pull out the data from the serialization store and hook it up to the
    control. Typically, there are issues with storing controls like this
    especially if they contain references or subhierarchies. I'm not entirely
    sure if this is fixed in 1.1 but it was a 1.0 issue.

    there is a thread in c# newsgroups about a year ago where paldino and i
    discussed this in some detail for a treeview control because i was
    experiencing some nasty issues so if you feel like it you can google around.

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
    "Owen Evans" <> wrote in message
    news:%...
    > This isn't a production app; I can happlly rewrite it for as long as I
    > want.
    > For the record, what *is* the right way?
    >
    > Owen
    >
    > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > news:...
    >> no this is not the right approach. but it is way too late to do it the

    > right
    >> way.
    >> and besides, if it works then carry on
    >>
    >> --
    >> Regards,
    >> Alvin Bruney
    >> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    >> Got tidbits? Get it here... http://tinyurl.com/27cok
    >> "Owen Evans" <> wrote in message
    >> news:...
    >> > The control I'm making is basically an editable datagrid. I'm

    > serializing
    >> > multiple instances of it to Session (Session != Cache, right?) so that
    >> > I
    >> > can
    >> > re-add them to the page after a postback.
    >> > What other method would you suggest?
    >> >
    >> >
    >> > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    >> > news:...
    >> >> why are you adding your control to cache?
    >> >> controls don't work right when serialized./deserialized from cache (at
    >> > least
    >> >> in 1.0 version) not sure if they addressed that for 1.1
    >> >>
    >> >> --
    >> >> Regards,
    >> >> Alvin Bruney
    >> >> [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    >> >> Got tidbits? Get it here... http://tinyurl.com/27cok
    >> >> "Owen Evans" <> wrote in message
    >> >> news:%...
    >> >> > INamingContainer is irrelevant - same error whether or not it's
    >> > declared.
    >> >> > This is a problem with the control itself and its relationship to
    >> >> > the
    >> >> > containing page, not the IDs of child controls.
    >> >> >
    >> >> > The problem was actually that when the controls are created, they're
    >> >> > always
    >> >> > created with an ID of _ctl0. So, when I pull a reference to the

    > control
    >> >> > out
    >> >> > of session state and add it back to the page, I end up adding

    > multiple
    >> >> > controls with the same ID. This confiuses ASP.NET, and so I end up

    > with
    >> > an
    >> >> > error.
    >> >> >
    >> >> > I didn't find a way to persuade ASP.NET to automatically create a
    >> >> > unique
    >> >> > ID,
    >> >> > so I'm now generating a Guid to use as a Session state key and

    > control
    >> > ID,
    >> >> > and explicitly assigning my controls an ID in the Init phase of the
    >> >> > page
    >> >> > lifecycle.
    >> >> >
    >> >> > I'm not sure that a Guid is the best/most elegant way to do it, but
    >> >> > it's
    >> >> > the
    >> >> > least amount of code required to generate something I know to be

    > unique
    >> >> > (hence the note "solved by hackery")..
    >> >> >
    >> >> > "cld" <> wrote in message
    >> >> > news:...
    >> >> >> [This followup was posted to
    >> >> >> microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was
    >> > sent
    >> >> >> to the cited author.]
    >> >> >>
    >> >> >> In article <>,
    >> >> >> says...
    >> >> >> > I've a web page that adds a custom control every time a button is
    >> >> > clicked.
    >> >> >> > I'm keeping a reference to each added control in Session state so

    > I
    >> > can
    >> >> >> > recreate existing controls every postback. However, the controls

    > are
    >> >> > always
    >> >> >> > added with ID "_ctl0" rather than "_ctl1", "_ctl2" ..., so I get

    > an
    >> >> > error:
    >> >> >> > "Multiple controls with the same ID '_ctl0' were found.

    > FindControl
    >> >> > requires
    >> >> >> > that controls have unique IDs". What do I need to do to get this

    > to
    >> >> > work?
    >> >> >> >
    >> >> >> > Thanks,
    >> >> >> >
    >> >> >> > Owen
    >> >> >> Perhaps you are missing a INamingContainer implementation. Please

    > let
    >> > me
    >> >> >> know if this helps you
    >> >> >
    >> >> >
    >> >>
    >> >>
    >> >
    >> >

    >>
    >>

    >
    >
    Alvin Bruney [MVP], Jun 10, 2004
    #10
    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. John M
    Replies:
    1
    Views:
    1,115
    Kumar Reddi
    May 29, 2005
  2. Guest
    Replies:
    6
    Views:
    370
    Walter Wang [MSFT]
    Dec 23, 2006
  3. Anirudha24.d
    Replies:
    0
    Views:
    1,696
    Anirudha24.d
    Feb 8, 2011
  4. Pradeep Mareddi
    Replies:
    1
    Views:
    271
    Hermit Dave
    Aug 30, 2004
  5. santoshp

    Dynamically adding multiple instances of same usercontrol

    santoshp, May 30, 2007, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    199
    santoshp
    May 30, 2007
Loading...

Share This Page