Last post Oct 13, 2008 03:49 PM by adamoneil1
Oct 13, 2008 12:30 PM|adamoneil1|LINK
I'm trying to build a server control called a RecordView--similar to a FormView, but mine uses the same template for both inserts and updates. (The built-in FormView control has separate insert and update templates.) I'm pretty new to server control development.
The meat of my CreateChildControls override is here:
So, you can see that an instance of RecordViewTemplate is data bound and added to the container's Controls collection.
Here's the relevant portion of my Render override:
All of this works when the RecordView is first viewed. The template content shows up--and it's data bound as expected. The problem is if I click any of the contained server controls within my RecordView, causing a postback,
the template content disappears and my _rvt (RecordViewTemplate variable) is null. I had thought that because I added it to the Controls collection it would always be there. So I'm confused. Any help greatly appreciated.
Oct 13, 2008 02:09 PM|joteke|LINK
you are also responsible for creating the controls on postback when there is no data source. Control instance is always recreated on postback ,meaning that Controls collection starts with being empty (declarative controls are added to it by the environment
- but for programmatic additions, developer is the solely responsible)
The logic should be somewhat - generally from databound control perspective:
- when databinding store the count of items to ViewState (otherwise the logic is pretty much as is e.g create controls and call DataBind)
- on postback (databinding is false), use the previously stored count to recreate the same amount of controls . but don't call DataBind.
If you have only one item always, there's probably no need to store the count but buold the logic to work in both cases. :-)
Oct 13, 2008 03:49 PM|adamoneil1|LINK
Thanks. I found a similar answer in this blog post