Asp. NET tips-Working with styles

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.