Thursday, 29 December 2011

.NET 4.0 - Set style of all the disable control using aspNetDisabled style

.NET 4.0 has feature of disable state, it is a CSS property which will apply style sheet on all disabled controls. The best thing about this feature is that there is no need to assign style to individual control, It gets applied to all disabled controls.
Step 1: Add a textbox, a button, a dropdownlist which will be enabled and disabled. Two more buttons which will enable and disable the textbox, button and dropdownlist.





Step 2: Add btnDisable_Click and btnDisable_Click method which will disable and enable the controls respectively.

protected void btnDisable_Click(object sender, EventArgs e)
{
TextBox1.Text = "Disabled";
TextBox1.Enabled = false;
Button1.Enabled = false;
DropDownList1.SelectedIndex = 1;
DropDownList1.Enabled = false;
}
protected void btnEnable_Click(object sender, EventArgs e)
{
TextBox1.Text = "Enabled";
TextBox1.Enabled = true;
Button1.Enabled = true;
DropDownList1.SelectedIndex = 0;
DropDownList1.Enabled = true;
}
Step 3: Add aspNetDisabled in Site.css. Make sure it should be aspNetDisabled.

.aspNetDisabled{
color:Yellow;
background-color:Maroon;
font-style:italic;
}
Now run the applicaiton, click on Disable button aspNetDisabled style will be applied to all the disbled controls without writing a single line of code.

Enabled Controls:
Disabled State - Enable

Disabled Controls:

Disabled State - Disable Like us if you find this post useful. Thanks!

Download Code

No comments:

Post a Comment