How to add Atlas ListSearch to a drop down via code

time to read 2 min | 287 words


I wanted to add this functionality to all my drop down lists, but it turned out that this is surprisingly difficult to do.
I am using the normal "inherit from all the controls that you are using" model, and I put the following inside my Drop Down class:

protected override void OnInit(EventArgs ignored)
{
    Page.RegisterRequiresControlState(this);
    Page.Load += delegate
    {
        extender.ID = "ListSearch_" + ID;
        extender.PromptCssClass = "invisible";
        extender.TargetControlID = "dummy";
        extender.ResolveControlID += delegate(object sender, ResolveControlEventArgs e) { e.Control = this; };
        AddExtenderToParentFormOrUpdatePanel();
    };
} 

private void AddExtenderToParentFormOrUpdatePanel()
{
    Control ctrl = Parent;
    while( !(ctrl is HtmlForm) && !(ctrl is UpdatePanel))
        ctrl = ctrl.Parent;
    UpdatePanel panel = ctrl as UpdatePanel;
    if(panel!=null)
    {
        panel.ContentTemplateContainer.Controls.Add(extender);
    }
    else
    {
        ctrl.Controls.Add(extender);               
    }

}