2012-09-21 75 views
1

我想擴展DropDownList以添加ListSearchExtender。擴展DropDownList以添加ListSearchExtender

使用下面的代碼,控制效果很好,但運行時在設計時它給我這個錯誤:

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.

我想了解這個錯誤的原因。

[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); 
    } 
} 

回答

1

我得到它通過簡單的方法,我不知道這是否會令問題在未來,但現在它工作得很好

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