日期:2014-05-18  浏览次数:20531 次

为何 ASP.NET 的 ListBox 控件的 Enabled 属性不起作用?
我在 ASP.NET 程序中使用 ListBox 控件,但是 Enabled 属性不起作用。示例代码如下所示:
HTML code

<%@ 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">
<head runat="server">
  <title>测试页</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:ListBox ID="lstTest" Rows="16" runat="server" />
  </div>
  </form>
</body>
</html>


C# code

using System;
using System.Web.UI;

namespace WebAppTester
{
  public partial class TestForm : Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      lstTest.DataSource = Enum.GetNames(typeof(ConsoleColor));
      lstTest.SelectedIndex = 4;
      lstTest.DataBind();
      lstTest.Enabled = false;
    }
  }
}


在上述代码中,我把 ListBox 控件的 Enabled 属设为 false,用于禁用该控件,不让用户选择该 ListBox 中的项目。
但是这没有起到作用,该 ListBox 还是可以进行选择。
请问这是怎么回事?应该如何解决?
谢谢!


------解决方案--------------------
C# code

<asp:ListBox ID="ListBox1" runat="server" Enabled=false Width="126px">
                        <asp:ListItem Value="sss"></asp:ListItem>
                        <asp:ListItem Value="sss"></asp:ListItem>
                        <asp:ListItem Value="sssff"></asp:ListItem>
                        <asp:ListItem></asp:ListItem>
</asp:ListBox>
可以, 
你把 
lstTest.DataSource = Enum.GetNames(typeof(ConsoleColor));
      lstTest.SelectedIndex = 4;
      lstTest.DataBind();
换成遍历添加试试。

------解决方案--------------------
试试纯 HTML的多选菜单 如果还不行说明你的机子浏览器有问题!
C# code

<select name="selectlist" size="23" disabled="disabled" multiple>
<option>123</option>
<option>123</option>
<option>123</option>
<option>123</option>
</select>

------解决方案--------------------

这个应该没问题啊。。。

4.0也不会有问题。。ListBox最后生成出来就是个SELECT标签而己

你用JS设置它的disabled="disabled"看看
------解决方案--------------------
<asp:ListBox ID="ListBox1" disabled="disabled" runat="server" >
<asp:ListItem >aaaa</asp:ListItem>
<asp:ListItem >bbbb</asp:ListItem>
</asp:ListBox>
------解决方案--------------------
探讨
我在 ASP.NET 程序中使用 ListBox 控件,但是 Enabled 属性不起作用。示例代码如下所示:
HTML code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="WebAppTester.TestForm" %>

<!DOCTYPE html PUB……

------解决方案--------------------
探讨

试了您提供了 HTML 代码示例,会起作用。
我查看了一下我的示例的页面源代码,如下所示:
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">