Search code examples
extendhtml.dropdownlistfor

Extend DropDownList to add ListSearchExtender


I want to extend DropDownList to add ListSearchExtender.

Using the code below, the control works well in runtime but in design time it give me this error:

SearchDropDownList - DdlTest There was an error rendering the control. Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request.

I'd like to understand the cause of this error.

[ToolboxData("<{0}:SearchDropDownList runat=\"server\"></{0}:SearchDropDownList>")]
public class SearchDropDownList : DropDownList
{
    private ListSearchExtender listSearchExt = new ListSearchExtender();
    protected override void OnInit(EventArgs e)
    {
        ReloadSettings();
    }

    protected override void Render(HtmlTextWriter w)
    {
        base.Render(w);
        listSearchExt.RenderControl(w);
    }

    public void ReloadSettings()
    {
        listSearchExt.TargetControlID = this.ID;
        listSearchExt.ID = this.ID + "_CalendarExtender";

        if (Controls.Count > 0)
        {
            foreach (Control item in Controls)
            {
                if (item.ID == listSearchExt.ID)
                {
                    Controls.Remove(item);
                }
            }
        }
        Controls.Add(listSearchExt);
    }
}

Solution

  • i got it by simple way i am not sure if it will make problem in future but for now it work well

        protected override void Render(HtmlTextWriter w)
        {
            base.Render(w);
            if (!this.DesignMode)
            {
                listSearchExt.RenderControl(w);
            }
        }