Recently visited the CCB website https://ibsbjstar.ccb.com.cn/V5/index.html#, (in fact, in the oblog,http://www.oblog.cn also used a similar technique, but the method is different)
You can find that when you choose a different text box, you can appear different colors, to complete this function is very simple, but if there are many similar features, if one set the control style is obviously very tired, a simple way to write a method for the system to execute on it, look at the following code:
class BasePage:Page
{
public static void SetInputControlsHighlight(Control container, string className, bool onlyTextBoxes)
{
foreach (Control ctl in container.Controls)
{
if ((onlyTextBoxes && ctl is TextBox) 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);
}
}
}
}
Define a class: The main thing is to rewrite onfocus and onblur, and when the user chooses a different control, a different style appears, and the style name needs to be defined by you.
Note here: This basepage is derived from the page class, so the page should be created later:
WebForm1:MyPage
{
//调用SetInputControlsHighlight方法
}
That's it, okay?