2009-12-03 94 views
0

我想通過在頁面中添加邊框來突出顯示頁面中使用的所有用戶控件。我們這樣做是爲了方便調試。我可以重寫用戶控件基類中的RenderControl方法,這很容易實現,但我們有很多不使用基類的用戶控件(遺留)。突出顯示ASP.NET頁面中的所有用戶控件

然後我採取了不同的方法。我嘗試遍歷所有頁面使用的頁面基類的PreRender方法中的頁面控件,併爲所有用戶控件添加一個帶邊框的div。但是我發現,如果包含代碼塊(即<%...%>),則無法添加或刪除控件集合。

有什麼建議嗎?

謝謝

回答

0

以下是如何突出顯示有焦點的活動輸入控件。您需要處理的onfocus的onblur輸入客戶端事件controls.and通過設置控制的className屬性

應用或刪除CSS樣式來控制添加到您的css文件:

.highlight 
{ 
    background-color: #fefbd2; /*highlight with yellow*/ 
    color: #000080;   /*make text blue*/ 
} 

在你的App_Code目錄創建一個輔助類像

public static class Helpers 
{ 
    /// <summary> 
    /// Adds the onfocus and onblur attributes to all input controls found in the specified parent, 
    /// to change their apperance with the control has the focus 
    /// </summary> 
    public static void SetInputControlsHighlight(Control container, string className, bool onlyTextBoxes) 
    { 
    foreach (Control ctl in container.Controls) 
    { 
    if ((onlyTextBoxes && ctl is TextBox) || 
      (!onlyTextBoxes && (ctl is TextBox || ctl is DropDownList || 
      ctl is ListBox || ctl is CheckBox || ctl is RadioButton || 
      ctl is RadioButtonList || ctl is CheckBoxList))) 
    { 
     WebControl wctl = ctl as WebControl; 
     wctl.Attributes.Add("onfocus", string.Format("this.className = '{0}';", className)); 
     wctl.Attributes.Add("onblur", "this.className = '';"); 
    } 
    else 
    { 
     if (ctl.Controls.Count > 0) 
      SetInputControlsHighlight(ctl, className, onlyTextBoxes); 
    } 
    } 
    } 
} 

然後,只需Ø verride任何頁面的OnLoad方法。

protected override void OnLoad(EventArgs e) 
{ 
    Helpers.SetInputControlsHighlight(this, "highlight", false); 
    base.OnLoad(e); 
} 
相關問題