how to reset control ID numbering when databinding

A

Andy Fish

Hi,

First some background: When you databind a repeater control, the controls
within the template are given an id like Repeater1:_ctl<n>:Button1 where <n>
increments for each repeater item. If you re-bind the control later on in
the page lifecycle, the contents are re-generated with the IDs starting from
0 again.

I have written my own templated control which mostly works except that if
you DataBind it more than once, the IDs don't start from 0 the second time.
So when a control inside the template causes a postback, the postback itself
happens but the control ID is not recognised and hence the OnBubbleEvent is
not called.

Can anyone explain where the control numbers are generated and how I get
them reset to 0 again?

TIA

Andy
 
G

Guest

Hi Andy,

all I can say about it is: The index is calculated in the
"CreateControlHierarchy" method of the Repeater. It's protected and virtual
so you could override it (although i would not say you have to).

The Repeater saves the amount of bound items in the ViewState?!?! Maybe this
could cause your Repeater not behave correctly.

Why should you "re-bind" the repeater later on the same page again?

cu patrick
 
A

Andy Fish

Patrick said:
Hi Andy,

all I can say about it is: The index is calculated in the
"CreateControlHierarchy" method of the Repeater. It's protected and
virtual
so you could override it (although i would not say you have to).

I am actually using my own custom control, not a repeater. I think the index
created in the repeater code is not the one that's used to generate the
control IDs - it's so the caller can find out the index.
The Repeater saves the amount of bound items in the ViewState?!?! Maybe
this
could cause your Repeater not behave correctly.

Why should you "re-bind" the repeater later on the same page again?

The reason I have to re-bind it is that I'm not using viewstate. I need to
bind it in the load event so that events get through correctly. But if a
click event changes the data, I need to bind it again before rendering.

Anyway, I think I have solved the problem. In my implementation of
OnDataBinding() I was clearing most of the child controls out but leaving
one behind (one that was always required). That was what was causing the
numbering to go wrong. Now I have fixed it so that control is also deleted
and created on each databind and it all seems to work fine now.
 

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

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top