我用三个ListBox控件做联动的分类程序,测试是已经通过了,可以的。
但是现在最烦人的是无法给选中的项改变文本颜色或增加背景色,以方便用户很直观的看到他所选择的分类。
请教一下大师们是怎么做的?我在网上怎么找都找不到相关资料呀!

解决方案 »

  1.   

    this.listBox1.DrawMode   =   DrawMode.OwnerDrawFixed;  
    然后在listBox1的DrawItem事件中处理。具体代码查一查DrawItem的帮助
      

  2.   

    因为选中的Item的Style是无法改变的.
    所以要自定义选中Item的Style.
    只有先将选中的Item的相关信息保存起来,然后再将它设为不选中,最后再设置此Item的Style.<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>未命名頁面</title>
        <script language="javascript" type="text/javascript">
       //Selected="True"
       function ListBox1_OnChange()
       {
        var ls = document.getElementById('ListBox1');
        //将所有Item的Style还原.
        for(var i=0;i<ls.options.length;i++)
        {
            ls.options[i].style.backgroundColor = 'white';
            ls.options[i].style.color = 'black';
        }
        //给选中的Item设定自定义的Style.
        ls.options[ls.selectedIndex].style.backgroundColor = 'yellow';
        ls.options[ls.selectedIndex].style.color = 'red';
        //先将选中的Index存到某个地方(比如:hidden,隐藏的Label......).
        
        //最后将SelectedItem清空.
        ls.selectedIndex = -1;
       }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="ListBox1" runat="server" onchange="ListBox1_OnChange()">
                <asp:ListItem>1</asp:ListItem> 
                <asp:ListItem >2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
            </asp:ListBox></div>
        </form>
    </body>
    </html>