Search code examples
asp.netcustom-server-controls

Populating a custom ASP.NET DDL Server Control


I am creating a simple server control which inherits from System.Web.UI.WebControls.DropDownList. This DDL will be populated by a list of languages.

I want to know on what event I should populate the DDL with the list from the Database.

OnLoad, OnInit or maybe in the constructor of the class? Does it make any difference?


Solution

  • You should always create and initialize dynamic server controls on Init page event.

    As explicitly stated here ("Use this event to read or initialize control properties") :

    http://msdn.microsoft.com/en-us/library/ms178472.aspx

    also this is a reccomended read :

    ASP.NET Page Life Cycle Overview

    ASP.NET Web Server Control Event Model

    http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx