Thursday, December 22

.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.

<asp:TextBox ID="TextBox1" Text="Enabled" runat="server"></asp:TextBox
><asp:Button ID="Button1" runat="server" Text="Button" /><asp:DropDownList ID="DropDownList1" runat="server">
   
<asp:ListItem Text="Enabled"></asp:ListItem
>
   
<asp:ListItem Text="Disabled"></asp:ListItem
></asp:DropDownList>
<asp:Button ID="btnDisable" runat="server" onclick="btnDisable_Click" Text="Disable" /><asp:Button ID="btnEnable" runat="server" onclick="btnDisable_Click" Text="Enable" />
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!
Shibashish mohanty

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

ShibashishMnty
shibashish mohanty