ASP. NET 4 ListBox control cannot be disabled

Source: Internet
Author: User
Problem description

Webform application in ASP. NET 4ProgramWhen the enabled attribute of The ListBox Server Control is set to false, the control is not disabled.

Test procedure

Testform. aspx:

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "testform. aspx. cs" inherits = "webapptester. testform" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">  

And the correspondingCodeHide the file testform. aspx. CS:

Using system; using system. web. ui; using system. web. UI. webcontrols; using skyiv. common; namespace webapptester {public partial class testform: pageex {protected void page_load (Object sender, eventargs e) {If (! Ispostback) {lsttest. datasource = enum. getnames (typeof (datetimekind); lsttest. selectedindex = 1; lsttest. databind () ;}} protected void button_click (Object sender, eventargs e) {Switch (button) sender) == btneanble);} void switch (bool isenable) {btndisable. enabled = isenable; btneanble. enabled =! Isenable; If (isenable) lsttest. Enable (); else lsttest. Disable ();}}}

The following is pageex. CS:

Using system; using system. web. ui; using system. web. UI. webcontrols; namespace webapptester {public abstract class pageex: Page {protected override void onloadcomplete (eventargs e) {base. onloadcomplete (E); rundisabledlistselectedlistitem (this);} void rundisabledlistselectedlistitem (control top) {foreach (control CTL in top. controls) {var lst = CTL as listcontrol; If (LST! = NULL &&! Lst. enabled) foreach (listitem item in lst. items) if (item. selected) item. attributes. add ("class", webcontrol. disabledcssclass); rundisabledlistselectedlistitem (CTL );}}}}

The above rundisabledlistselectedlistitem method is used in combination with main.css to remedy bugs in IE 8, ie 9, and chrome browsers. Webcontrolextensions. CS:

Using system. web. UI. webcontrols; namespace skyiv. common {public static class webcontrolextensions {static readonly string tagdisabled = "disabled"; public static void enable (this webcontrol CTL) {CTL. enabled = true; CTL. attributes. remove (tagdisabled);} public static void disable (this webcontrol CTL) {CTL. enabled = false; CTL. attributes. add (tagdisabled, tagdisabled );}}}

The above disable extension method is used to remedy that The ListBox server control cannot be disabled.

Finally, the style sheet main.css:

 
Option. aspnetdisabled {color: White; Background-color: #00 FFFF ;}
Running result

The running result of the above test program is as follows:

Click "Disable:

Corresponding HTML pageSource codeAs follows:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

We can see that the select HTML element of Line 1 in the above HTML source code has added the disabled attribute, and the option sub-element of Line 2 has also added the class attribute to correspond to the corresponding style in main.css, fix bugs that cannot be correctly displayed in IE 8, ie9, and chrome 19.0 browsers.

Description in msdn

As mentioned in ASP. Net 4 and Visual Web Developer in msdn:

 
CSS used to disable controlsWhen a control is disabled in ASP. NET 3.5 (see webcontrol. enabled), a disabled attribute is added to the rendered HTML element. For example, the following tag creates a disabled label control: <asp: label id = "label1" runat = "server" text = "test" enabled = "false"/> in ASP. in net 3.5, the original control settings generate the following HTML: <span id = "label1"> test </span> in HTML 4.01, the disabled feature is considered invalid for the span element. It is only valid for input elements because it specifies that these elements cannot be accessed. For only display elements (such as span), browsers usually support a disabled appearance. However, according to the standard of auxiliary functions, webpages dependent on such non-standard behaviors are not reliable. For elements that are only for display, CSS should be used to specify the disabled visual appearance. Therefore, by default, Asp. net 4 generates the following HTML for the control settings shown above: <span id = "label1" class = "aspnetdisabled"> test </span> by setting the disabledcssclass attribute, you can change the default class attribute value when the control is disabled.

In fact, in ASP. NET 4, if the enabled attribute of webcontrol is set to false, the Select HTML element corresponding to the ListBox Server Control is indeed not added with the disabled attribute. The disabled attribute is added to the input HTML element corresponding to the button Server Control. The Select HTML element cannot be a display-only element.

Use ASP. NET 3.5 compatibility mode

If you use ASP. NET 3.5 compatibility mode to present pages, you can also solve the problem by setting the following settings in the web. config file:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <system. Web> <pages controlrenderingcompatibilityversion = "3.5"/> </system. Web> </configuration>

However, the advantages of ASP. NET 4 cannot be used. It is not recommended.

References
    1. Stackoverflow: ASP. net c # ListBox server control will not disable
    2. Csdn: Why does the enabled attribute of The ListBox control of ASP. NET not work?
    3. Msdn: new functions in ASP. NET 4 and Visual Web Developer
    4. Blog: Display of the disabled attribute of the HTML select element in mainstream browsers
Related Article

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.