views:

31

answers:

1

Here is a quick code snippet, that doesn't seem to work at all for me. I'm reading from a file to create a list of radio buttons. The problem is that when one of the radio buttons is clicked the Event Handler I have set up in the code doesn't fire. I have tested it over and over in debug mode with line breaks... all with no luck. Am I missing something obvious here????

Thanks in Advanced!

 strLine = strLine.Trim();
 System.Diagnostics.Debug.WriteLine("[3-a] ship by date - date: " + strLine);

 try{ shipByDate = (Convert.ToDateTime(strLine)); }
 catch (Exception e) { shipByDate = new DateTime(); }

 shipByDesc = sr.ReadLine().Trim();
 System.Diagnostics.Debug.WriteLine("[3-b] ship by date - desc: " + shipByDesc);

 RadioButton button = new RadioButton();
 button.Text = shipByDesc + " - " + shipByDate.ToString("MM/dd/yyyy");
 button.Checked = false;
 button.GroupName = "shipByOptions";
 button.ID = "shipByRadio" + count;

 //button.EnableViewState = true;
 button.AutoPostBack = true;
 button.CheckedChanged += new EventHandler(shipBy_CheckedChanged); // <-- doesn't work!!!

 //form1.Controls.Add(button);
 shipByPlaceHolder.Controls.Add(button);
+3  A: 

You need to add the button on every postback before events attached to it will fire.

If you think about it for a moment, it will make sense - if the button has not been created (on the postback), then there are no button events that can fire. The button must exist before events attached to it can be fired.

The OnInit page event is the most suitable place to add dynamic controls to a page.

Read about the asp.net page life cycle.

Oded
You rock Oded, thanks for the help. That was exactly the problem.
Ryan
@Ryan don't forget to appoint him a "checkmark" then :)
Rune FS
Got it, sorry I'm a bit new to SO. Check check check
Ryan