简体   繁体   English

ASP.NET C#ListBox服务器控件不会禁用

[英]ASP.NET C# ListBox server control will not disable

I have 4 server side ListBox controls. 我有4个服务器端ListBox控件。 All of them have their Enabled property set to false, yet when rendered they are definitely enabled. 所有这些都将其Enabled属性设置为false,但在渲染时它们肯定已启用。 They are all multiple select. 它们都是多选的。 These have no data binding or any code behind touching them. 它们没有数据绑定或触摸它们的任何代码。 Below is the markup for all of them (save the ID). 下面是所有这些标记(保存ID)。 I am running v4 of the .NET Framework with IIS6. 我正在使用IIS6运行.NET Framework的v4。

<asp:ListBox runat="server" ID="lstProduct" Enabled="false" SelectionMode="Multiple" Rows="6"></asp:ListBox>

Here is the markup that is generated by the runtime: 这是运行时生成的标记:

<select size="6" name="ctl00$ctl00$MainContent$MainContent$lstProduct" multiple="multiple" id="MainContent_MainContent_lstProduct" class="aspNetDisabled">

I found a solution. 我找到了解决方案。 In the <system.web> section of web.config, you must add <pages controlRenderingCompatibilityVersion="3.5"> . 在web.config的<system.web>部分中,您必须添加<pages controlRenderingCompatibilityVersion="3.5">

With Asp.net 4.0, any control that does not take specific user input (textbox or password), will not be rendered with a disabled="disabled" attribute when Control.Enabled = false is set. 使用Asp.net 4.0时,如果设置了Control.Enabled = false ,则不会使用disabled="disabled"属性呈现任何不接受特定用户输入(文本框或密码)的Control.Enabled = false

Try this: 尝试这个:

protected void Page_Load(object sender, EventArgs e)
{
  if (!this.IsPostBack)
  {
   this.lstProduct.Attributes.Add("disabled", "");
  }
}

To remove it you can just remove the disabled tag like this: 要删除它,您可以删除禁用的标记,如下所示:

this.lstProduct.Attributes.Remove("disabled");

Write the following line in the .cs file 在.cs文件中写下以下行

ListBox.Attributes.Add("disabled", "true"); ListBox.Attributes.Add(“disabled”,“true”);

A better solution is to inherit from the ListBox class and then override the SupportsDisabledAttribute property. 更好的解决方案是从ListBox类继承,然后重写SupportsDisabledAttribute属性。 Detailed information can be found in MSDN library 详细信息可以在MSDN库中找到

eg 例如

public class MyListBox : ListBox
{
   public override bool SupportsDisabledAttribute { get { return true; } }
}

This should be considered a bug in the .Net Framework. 这应该被视为.Net Framework中的一个错误。

http://www.asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc256770141 says: http://www.asp.net/whitepapers/aspnet4/breaking-changes#0.1__Toc256770141说:

Controls that are not designed for user input (for example, the Label control) no longer render the disabled="disabled" attribute if their Enabled property is set to false (or if they inherit this setting from a container control). 如果将Enabled属性设置为false(或者如果它们从容器控件继承此设置),则不为用户输入设置的控件(例如,Label控件)不再呈现disabled =“disabled”属性。

Also see rationale for the change (rendering valid html) at http://msdn.microsoft.com/en-us/library/system.web.ui.control.renderingcompatibility.aspx . 另请参阅http://msdn.microsoft.com/en-us/library/system.web.ui.control.renderingcompatibility.aspx上的更改(呈现有效的html)的基本原理。

But a list box is designed for user input and the disbled attribute is supported in html, so it ought to render disabled="disabled" . 但是列表框是为用户输入而设计的,html支持disbled属性,因此它应该呈现disabled="disabled"

You can use a little jquery as a bandaid until this is properly fixed. 在正确修复之前,您可以使用一个小jquery作为bandaid。 If you put this somewhere that's run for all pages it will fix it for all disabled listboxes on all pages: 如果你把它放在为所有页面运行的某个地方,它将为所有页面上的所有禁用列表框修复它:

$(document).ready(function () {
    $("select.aspNetDisabled").attr('disabled', 'disabled');
});

You may wish to instead disable the options within the select box, as this will allow scrolling. 您可能希望禁用选择框中的选项,因为这将允许滚动。

//Listbox cannot be disabled directly, instead the inners should be disabled instead.
foreach(ListItem item in lbCategory.Items)
{
    item.Attributes.Add("disabled", "disabled");

    if (item.Selected)
    {
        //cannot reliably style with [disabled='disabled'][selected='selected'] or :checked:selected etc, so need a class
        item.Attributes.Add("class", "disabledSelected"); 
    }
}

I then use the following CSS, so the user can still see preselected items. 然后我使用以下CSS,因此用户仍然可以看到预先选择的项目。

/* Slightly lighter colour than the normal #3399FF because you cannot change the foreground color in IE, so means that it isn't contrasted enough */
select option.disabledSelected { background-color: #97cbff !important} 

Unfortunately from my initial investigations it's a bit of a pain to style disabled input elements in a nice cross browser way. 不幸的是,从我最初的调查来看,以一种很好的跨浏览器方式设置禁用的输入元素是一件很痛苦的事情。 I've setteled with using a class for my purposes, however this article regarding styling disabled form elements might help . 我已经为我的目的使用了一个类,但是关于样式禁用表单元素的这篇文章可能有所帮助

You may also notice that in IE, click events will still be triggered, which seemed to deselect the options but only in some combinations of trying to use [disabled='disabled'][selected='selected'] or :checked:selected etc. 您可能还会注意到,在IE中,单击事件仍将被触发,这似乎取消选择了选项,但仅在尝试使用[disabled ='disabled'] [selected ='selected']或:checked:selected等的某些组合中。

I had the same problem but with CheckBoxList . 我有同样的问题,但使用CheckBoxList

Setting its Enabled property to false didn't disable it. 将其Enabled属性设置为false不会禁用它。 The panel it was inside of would also not have an effect on it when Enabled = false . Enabled = false时,它所在的面板也不会对它产生影响。

The solution was to use a foreach loop over the items in the CheckBoxList. 解决方案是在CheckBoxList中的项目上使用foreach循环。

foreach (var item in checkBoxList.Items.Cast<ListItem>())
{
    item.Enabled = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM