Dynamic controls not added on postback

S

s.bussing

Hi, I have been reading for hours now, but I'm still not able to solve
my problem, though a lot of people are struggling with something
similar.

On my page I have a nested repeater. In the inner repeater for each
item a usercontrol is added which has a textbox and two buttons.

No I know that when the page is posted back (in my case by pushing one
of the buttons of the control), I have to recreate all the dynamic
control. I want to maintain the state of the controls, but thats the
second part. The first part is recreating the dynamic added controls.
Therefor I have a function which I call in the Page_Load. First I
check if the page is postedback if so, I call this function:

Private Sub ReloadDynamicControls()
Dim _Ctrl As Control = CustomControl()
Dim _Repeater As Repeater = Me.FindControl("rptFactor")
For Each OuterItem As RepeaterItem In _Repeater.Items
For Each InnerItem As RepeaterItem In
CType(OuterItem.FindControl("rptFactorItem"), Repeater).Items

InnerItem.FindControl("pnlToelichtingGedrag").Controls.Add(_Ctrl)
Next
Next
End Sub

But when the page is displayed it only shows the usercontrol for the
last items of the repeater. But it should show for every item. When
I'm debugging I can see that the items are there and the usercontrol
is added to the placeholder, at least that is what I guess. But it
never shows on the page anymore.

Any helps is highly appreciated.
Thx
 
M

Mark Rae [MVP]

No I know that when the page is posted back (in my case by pushing one
of the buttons of the control), I have to recreate all the dynamic
controls.

That's correct.
Therefore I have a function which I call in the Page_Load.

Generally speaking, Page_Load is too far down the page cycle for creating
dynamic controls, especially if you're hoping to wire them up to events.

Try creating the dynamic controls in Page_Init instead...
 
S

s.bussing

The Repeater like its brethren DataGrid, DataList, et all all have an
ItemDataBound or RowDataBound event which you can wire up to add in your
controls. As Mark pointed out, Page_Load is usually way too late in the page
cycle to expect to be able to do it there.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

Hi Peter and Mark,

thanks for the reply. Actually I tried the page_init, but in the
example I put in the repeater items are not know a that stage. They
are only known when I bind to the controls first. This means that I
have to place the databind action into the init too. Correct?
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top