Items in the repeater are lost during postback (callback)

Using ASP.NET 4.0 WebForms. I have a repeater with checkboxes. The repeater and checkboxes have view mode enabled. There's a button that triggers a callback postback. These items are in the callback panel (similar to MS UpdatePanel, but don't use viewstate). The repeater is bound to data in place at boot time, and the repeater shows flags. Still.

During postback (callback) I noticed that the cardinality of the relay is 0 in page_load. So I cannot get the checkbox values. I can see the key / checked value entries of the checkboxes in the Request.Form collection.

I think I am missing something obvious, but where in the lifecycle can I read the repeater items? Or should I get them from Request.Form?

+3


source to share


2 answers


1) Try connecting to LoadComplete. All child controls are loaded recursively, so sometimes not everything you expect to see will be present during the Load event.

2) Make sure your repeater is initialized during or before the Init event. If you have any code that runs to populate it, this should be done in Init. During the life cycle of a page, ASP.NET tries to take published values ​​and apply them to controls. If controls are not created quickly enough during their lifecycle, they will not exist at this point to accept the published values. This is one of the more frustrating things with dynamic pages in ASP.NET, as you have to make sure that the page is restored during init postback. So even when you see data in a message, ASP.NET ignores it if no controls are found in its control collection. Also, there is some magicwhich happens to the control IDs it uses to map the published values ​​to the controls. I do not remember the details of this, because it has been several weeks since I had to delve into the details of Harry.



Also, make sure you don't do something like using if (! IsPostBack) to initialize a repeater or other controls / data. Even if it is a postback, you still need the controls to accept the posted values.

I haven't used a callbackpanel though, so I'm not sure how this would muddy the waters.

+3


source


I solved my problem by overriding the method DataBind

in the control that contains the relay.
This prevents leakage DataBind

on parent controls and data bindings to the relay when it is not "t ready for it. My problem was that the parent control / page was executing a DataBind that flocked to sub controls, including the reppater, forcing . it cleaned this way, you do not have to do DataBind

directly to the repeater at each callback, for example, @AaronLS offers:

Also, make sure you don't do something like using if (! IsPostBack) to initialize your relay or other controls / data. Even if it's a postback, you still need the controls to exist in order for them to accept the posted values.



All of my RepeaterItems stayed present through postbacks even though I only bind data once.

In my case, executing DataBind

on a repeater for each postback caused the form values ​​to be reset.

+1


source







All Articles