Return to doc.sitecore.com

Valid for Sitecore 5.2, 5.3
Why the fields of the control are empty?

When you add the controls to the parent container, it is important to add the control first, and populate its fields only after that.

Right:

ListItem listItem = new ListItem();
listbox.Controls.Add(listItem);
listItem.Header = "header";
listItem.Value = "value";

Wrong:

ListItem listItem = new ListItem();
listItem.Header = "header";
listItem.Value = "value";

listbox.Controls.Add(listItem);

In the latter case, the .Header and .Value properties of the list item will be empty after postback or event. The reason is the ASP.NET will not persist these properties in the view state if you modify them before adding the control.