The _id of controls

L

Lenard Gunda

Hi!

I noticed, that the postback instructions generated for controls use the _id
attribute of controls, that seem to generated by asp.net itself. My problem
with this is the following.

I have a page which creates some LinkButtons dynamically in Page_Load. They
need to be created there, in order for the postback mechanism and events to
work. However, in some circumstances, I need to recreate them later (if the
user presses one of them). And here comes the problem. When I later delete
and recreate them, the _id numbering does not restart, rather continues.
(for example, assuming I created 4 buttons in Page_Load, I later delete
them, create 4 buttons again, however, this time the numbering starts from
5, not 1).

What this means, is that next time a postback occurs, the control _id -s are
wrong, and the page doesn't do a thing - except generates itself again. To
continue the previous example, the controls with _id -s 1 to 4 are created
in Page_Load, and because postback events are not handled, are now displayed
on the page. Now you can click on them, but the next version again generates
_id -s with 5 to 8.

So, here's it again. Between each of the postbacks, the user has to click on
one of the four LinkButtons.

First generation, no postback
- Page_Load creates 4 buttons with id-s 1-4
- User presses one of the buttons (1-4)

Second generation, postback
- Page_Load creates 4 buttons with id-s 1-4, later these are destroyed, and
the user gets a page with button id-s 5-8
- User presses one of the buttons (5-8)

Third generation, postback
- Page_Load creates 4 buttons with id-s 1-4, because there are no buttons
with id-s 5-8, nothing happens, because no control is found to handle the
events.
- User presses one of the buttons (1-4)

Fourth generation, postback
- Page_Load creates 4 buttons with id-s 1-4, and this time they are found
and handled.

Is there a solution to this problem? Can I somehow reset the control
counting (numbers) of the page, because removing/deleting the controls does
not seem to have an effect. The controls are removed before any rendering is
done (and I also tried calling Dispose() on them, without any effect), but
this does not restart the numbering.

Thanks for any ideas

-Lenard
 
D

David Alexander

Lenard,

You don't have to let ASP provide IDs for you. The ID of each control is a
property that you can set. Rather than letting ASP generate IDs, try
explicitly naming them yourself, so you can have the consistency that you
want.
 

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
474,139
Messages
2,570,805
Members
47,352
Latest member
DianeKulik

Latest Threads

Top