Problem adding multiple instances of a custom control to aspx page

O

Owen Evans

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
 
O

Owen Evans

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).
 
C

cld

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

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
 
O

Owen Evans

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 said:
[This followup was posted to
microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was sent
to the cited author.]

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
 
A

Alvin Bruney [MVP]

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 said:
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 said:
[This followup was posted to
microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was sent
to the cited author.]

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
 
O

Owen Evans

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 said:
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 said:
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 said:
[This followup was posted to
microsoft.public.dotnet.framework.aspnet.webcontrols and a copy was sent
to the cited author.]

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
 
A

Alvin Bruney [MVP]

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 said:
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 said:
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 said:
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")..

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

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
 
O

Owen Evans

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 said:
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 said:
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 said:
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
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")..

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

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
 
A

Alvin Bruney [MVP]

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 said:
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 said:
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 said:
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
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
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")..

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top