Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with viewstate of dynamic controls inside a repeater

Tags:

c#

asp.net

I ran into a problem recently when using a repeater that I was adding dynamic controls into and although I've got a workaround that does functionally exactly what I want it to do, I'd like to know if there is a better way to do it for my understanding. I've been working with ASP.NET for about 6 months now, and everytime I think I've got the page lifecycle/viewstate completely sussed something crops up that I can't answer.

I was creating a form where users could register their interest for an event, and register for multiple people.

The aspx went something like:

<asp:Repeater ...>
    <bunch of formatting and always there controls like firstname/lastname/address>
    <asp:PlaceHolder ...>
        <dynamic controls for workshop selection go here>
    </asp:PlaceHolder>
</asp:Repeater>

An event can have workshops that the user can register for, and the availability of the workshops is dependant on the date that they choose to go to the event on. The availability of the workshops is dependent on the date, so they can't choose the workshops until they've selected a date.

Anyway the dynamic controls that I'm adding are basically a bunch of literals and a bunch of radio button groups.

I started off by adding the controls in the ItemDataBound event handler, but when saving my repeater items back to my delegate list the ViewState of the radio buttons was never updated. All the fields that were referenced in the ItemTemplate were handled fine, but not the radio buttons I was adding dynamically.

I tried to use the ItemCreated event instead, adding my buttons there, but that didn't seem to make any difference. In the end I settled on a workaround which was based off of this. Basically I'm just outputing the HTML input fields in a literal and reading them back from the request.

It all works perfectly now, and I'm happy with the functionality, it just seems really dirty to have to output the radio buttons as HTML inputs directly rather than use a server side control. Does anyone have a clue about why the ViewState wasn't being restored properly?

Just to be clear, the controls were recreated in the same order everytime, with the ID properly set. I'm using dynamic controls all over the place and they're working fine, I just can't get them to work in this case when I'm adding them inside a repeater.

//more clarification

I can't create these controls in Page.Init as the user selects the date which causes a postback and I have to wait for the viewstate of that control to load before I create the dynamic controls.

like image 474
Andrew Barrett Avatar asked Feb 22 '09 18:02

Andrew Barrett


1 Answers

You're creating (or re-creating) the controls when the repeater is bound to its data source. If this happens after the ViewState has been loaded by the page, the ViewState won't be available to the dynamically-created controls.

Check you're binding your repeater early enough - Page.Init is OK; Page.Load is too late.

like image 83
teedyay Avatar answered Sep 28 '22 23:09

teedyay