how to persist event handlers for dynamically created web controls



i've got a script that loops through a dataset and creates dynamic web
controls and event handlers:

while (oDr.Read()){
RadioButton oRb = new RadioButton();
oRb.ID = oDr["id"].ToString();
oRb.AutoPostBack = true;
oRb.CheckedChanged += new

once any of the resulting radio buttons are clicked, a postback fires and my
web controls are gone as well as the event handlers upon the page reload.
the fact that the web controls are now gone doesn't really matter in this
particular case, but the event handlers themselves are also gone, so my
script doesn't execute. i was considering throwing the dynamically created
controls into a table, saving that to session, and recreating upon the page
load, but i can't save the event handlers there. the other thing to do would
be to rerun the sql statement that was responsible for creating the dataset
in my pageLoad event, but that seems like a waste of resources.

has anybody got a better way to deal with this kind of thing?




bruce barker

you should do the recreate in oninit not onload. nothing wrong with
redoing the sql query. your other option is to cache the dataset in
session, but if you are using sql for session (enterprise level site)
then its not much quicker.

-- bruce (