2011-06-27 23 views
0

我正在構建基於指定狀態在頁面上呈現HTML的複合控件。複合控件中的面板忽略可見性

如果我設置了控件,並將其添加到ControlCollection進行合成,並在設置爲false時設置控件的可見性,它似乎工作正常,面板隱藏,直到頁面上的回傳導致要顯示的面板。

但是,當我在Render方法中包裝RenderBeginTag(writer)RenderEndTag(writer)時,它似乎忽略了初始化過程中的「visible = false」語句?

// initialization 
this._contentPanel = new Panel(); 
this._contentPanel.ID = "ContentPanel"; 
this._contentPanel.Visible = false; 
this.Controls.Add(this._contentPanel); 

// CreateChildControls 
this.InitContentPanel(); // adds the content panel to control collection 

// render 
this._contentPanel.RenderBeginTag(writer); 
writer.WriteLine("<div>Some copy here</div>"); 
this._contentPanel.RenderEndTag(writer); 

這基本上仍然顯示面板,無論初始化過程中的可見性檢查。我已經測試過各種不同的場景,出於某種原因,這只是忽略了狀態。有任何想法嗎?

謝謝,

埃裏克

回答

2

看得見的標誌確定是否控制在服務器上渲染。因此,在CreateChildControls期間添加控件時,ASP將檢查可見標誌並在Render()期間跳過該控件。但是,當您調用RenderBeginTag時,您實際上忽略了Visible標誌。

如果要將控件HTML呈現給客戶端,但保留隱藏的div,則應將display CSS屬性設置爲none。

例如

this._contentPanel.ID = "ContentPanel"; 
this._contentPanel.Visible = false; 
this._contentPanel.Style["display"] = "none";