0

This is a little difficult to explain so please bear with me.

I have a procedure that is generating some radio buttons and assigning a CheckedChanged postback event based on the level being passed through (up to 4 levels). When the first level is checked (radio button selected) the postback event rb_CheckChanged00() is called and a check is done to see if this item has any children, if it does, it will create more radio buttons and assign rb_CheckChanged01 to the CheckChanged event for these - This part is working fine.

The issue I have is when I select the second Radio Button that has been created (the child), it doesn't seem to go to the post back event at all. The page is posting back when I click on it but everything resets because it won't go into rb_CheckChanged01.

I know this info is quite vague but I am hoping someone has an idea on how the post back event works and if I am somehow using it incorrectly.

Using: ASP.NET 2.0, IIS7

Thanks.

webnoob
  • 15,747
  • 13
  • 83
  • 165

4 Answers4

2

Most of the time when the dynamically created control's events are not fired, it's because the controls are 'reset' upon postback.

To make sure the same controls get created each and every time make sure that the control's IDs are set to the same values each and every time, before the ViewState is loaded. This way, when the control is added to the control collection of the page, once the ViewState is loaded, it'll persist it's properties. (just to describe what happens, in a nutshell)

One of the best articles I've read on this topic is this one. Make sure you read it, to fully understand what's happening in the background.

Adam Vigh
  • 1,260
  • 2
  • 13
  • 20
  • Sorted. I am now generating my Radio Buttons on each postback based on the ID's stored in a session. They have exactly the same ID's as when they were posted and all is working. Thanks for your help. – webnoob Dec 06 '10 at 11:26
1

Looks like the child RBs are cleaned before they are able to trigger the event. From my personal experience, it's best to keep track of those dynamically generated objects, and regenerate them in every postback. The events will start to trigger :)

Davita
  • 8,928
  • 14
  • 67
  • 119
1

Your controls and events are not registered in the ViewState because dynamic controls need to be loaded in the Page_Init. Because they're not persisted in the ViewState, they won't be registered with events. A similar question:

Problem with dynamic controls in .NET

Community
  • 1
  • 1
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
0

Only 1 thing can cause this, you create the rb's on page_load and don't add them to a List<> or something similar and that object to Session. What you need to do is when you create the items, Add them to a List and add that list to Session["RadioButtons"] and if the Page.IsPostBack is true, load your controls one by one from your list which is kept in your session to your page.

Pabuc
  • 5,528
  • 7
  • 37
  • 52