7

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.

Andrew Barrett
  • 19,721
  • 4
  • 47
  • 52
  • Does this answer your question? [Maintaining viewstate of a repeater](https://stackoverflow.com/questions/7416421/maintaining-viewstate-of-a-repeater) – Michael Freidgeim Mar 06 '20 at 08:39
  • @MichaelFreidgeim honestly it's been so long since I've dealt with web forms I don't know! I haven't worked in .NET let alone web forms for years. My advice now to anyone having to use web forms would probably be just to turn of view state altogether to be honest. – Andrew Barrett Jun 18 '20 at 20:02
  • Sorry for the automatically created unintentional question - the comment should be [“Possible duplicate”](https://meta.stackexchange.com/questions/339563/the-auto-comment-does-this-answer-your-question-generated-when-voting-to-clos) – Michael Freidgeim Jun 18 '20 at 20:11
  • Ah - in this case it doesn't, the flow of the problem was different so the answer in the other question doesn't help here. – Andrew Barrett Jun 18 '20 at 20:20

2 Answers2

5

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.

teedyay
  • 23,293
  • 19
  • 66
  • 73
  • 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. Besides Page_Load is not too late, ASP.NET is supposed to run through the whole page lifecycle ... – Andrew Barrett Feb 22 '09 at 19:11
  • ... when adding a new control. At least I thought it was supposed to. I can add dynamic controls in page_load and have it work, just not when they are in the repeater. – Andrew Barrett Feb 22 '09 at 19:12
0

It is worth noting that Radiobuttons controls do not work 'out of the box' in repeater controls. Repeater controls inherit the INamingContainer interface which ensures all rendered html controls have unique name attributes. As radio buttons use the name attribute to group themselves this means you will not be able to create groups of radio buttons i.e. setting the GroupName property will not have the desired effect as the Repeater will override this and create unique names for each RadioButton.

Andy Rose
  • 16,770
  • 7
  • 43
  • 49
  • Yep, I know that, which is why I probably would have to use my workaround regardless. I just really want to know why I can't get the viewstate working. – Andrew Barrett Feb 23 '09 at 10:53