以下是如何突出顯示有焦點的活動輸入控件。您需要處理的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);
}