Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Problem adding multiple instances of a custom control to aspx page

Reply
Thread Tools

Problem adding multiple instances of a custom control to aspx page

 
 
Owen Evans
Guest
Posts: n/a
 
      06-07-2004
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


 
Reply With Quote
 
 
 
 
Robert Koritnik
Guest
Posts: n/a
 
      06-07-2004
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
>
>



 
Reply With Quote
 
 
 
 
Owen Evans
Guest
Posts: n/a
 
      06-07-2004
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
> >
> >

>
>



 
Reply With Quote
 
cld
Guest
Posts: n/a
 
      06-07-2004
[This followup was posted to
microsoft.public.dotnet.framework.aspnet.webcontro ls 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
 
Reply With Quote
 
Owen Evans
Guest
Posts: n/a
 
      06-08-2004
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.webcontro ls 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



 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      06-08-2004
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.webcontro ls 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

>
>



 
Reply With Quote
 
Owen Evans
Guest
Posts: n/a
 
      06-09-2004
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.webcontro ls 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

> >
> >

>
>



 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      06-09-2004
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.webcontro ls 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
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
Owen Evans
Guest
Posts: n/a
 
      06-10-2004
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.webcontro ls 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
> >> >
> >> >
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      06-10-2004
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.webcontro ls 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
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User control multiple instances in page + common javascript functionality Anirudha24.d ASP .Net 0 02-08-2011 05:03 AM
Dynamically adding user controls, and then setting custom properties on those instances? ASP .Net 6 12-23-2006 09:12 AM
adding main.aspx.vb & main.aspx.resx under aspx John M ASP .Net 1 05-29-2005 09:27 PM
How Can I use multiple instances of the same custom control on an ASP.NET Page? Pradeep Mareddi ASP .Net Web Controls 1 08-30-2004 06:50 PM
adding runat=server to a control on aspx page, don't cause the IDE to add the control to the code behind z. f. ASP .Net 1 06-25-2004 03:15 AM



Advertisments
 



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