简单升级了一下可着色的ListBox控件

    记得去年年初的时候做了一个Colorful ListBox Control控件。当时考虑的因数很少,虽然那个控件实现了简单的调用接口,可是有一个致命的问题是居然没有考虑到PostBack后的状态保存问题

    新的ColorableListBox解决了控件被PostBack后的状态保存。实现过程很简单,就是把ListItem的ForeColor和BackColor信息存在ViewState里面,自己在控件中去维护。这个功能本来是很简单的,可是在做完后发现也仍然并不完美,因为目前还是不能处理条目移动的问题,如果条目被移动,比如有删除和插入等操作等,ListItem的颜色不能同步。目前的处理办法是如果有ListItem的变动,就清空所有的色彩信息。

    执行流程,在控件的OnPerRender事件运触发时,判断ListBox的Item的Attributes.CssStyle属性里是否被设置了color和background-color属性。如果有就记录下其属性值,然后保存在ViewState里,PostBack后从ViewState里恢复Item的属性值,在Render事件里,使用自己保存的属性信息来修改<option ...>...</option>的css属性。 

protected override void OnPreRender(EventArgs e)
{
    
if ( m_IsClear )
    
{
        m_ItemStyles 
= null;
    }


    ArrayList alstStyle 
= null;
    Hashtable htItemStyles 
= null;

    
forint i=0 ; i < this.Items.Count ; ++i )
    
{
        
if ( alstStyle != null )
        
{
            alstStyle.Clear();
        }

        
foreachstring key in this.Items[i].Attributes.CssStyle.Keys )
        
{
            
if ( htItemStyles == null )
            
{
                htItemStyles 
= new Hashtable();
            }

            
if ( m_ItemStyles != null )
            
{
                
foreachobject obj in m_ItemStyles )
                
{
                    
object [] objs = (object [])obj;
                    htItemStyles[objs[
0]] = objs[1];
                }

            }

            
if ( alstStyle == null )
            
{
                alstStyle 
= new ArrayList();
            }

            
string strKey = key.ToLower();
            
if ( strKey == "color" || strKey == "background-color" )
            
{
                alstStyle.Add(key 
+ ':' + this.Items[i].Attributes.CssStyle[key]);
                
continue;
            }

        }

        
if ( alstStyle != null && alstStyle.Count > 0 )
        
{
            
string [] strAry = new string[alstStyle.Count];
            alstStyle.CopyTo(strAry);
            htItemStyles[i] 
= strAry;
        }
            
    }

    
if ( htItemStyles != null )
    
{
        
int count = 0;
        m_ItemStyles 
= new object[htItemStyles.Count];
        
foreachobject key in htItemStyles.Keys )
        
{
            
object [] objects = new object[2];
            objects[
0= key;
            objects[
1= (string [])htItemStyles[key];
            m_ItemStyles[count
++= objects;
        }

    }

    
base.OnPreRender(e);
}

    控件的使用方法为:
 ColorableListBox clb = new ColorableListBox();
 ListItem li 
= new ListItem(name, value);
 li.Attributes.CssStyle.Add(
"color""yellow");
 li.Attributes.CssStyle.Add(
"background-color""blue");
 clb.Items.Add(li);
    如果重新设置条目后(比如有Insert和Delete操作等)需要调用一下:ClearItemStyles();

附ColorableListBox控件源码

posted on 2005-01-01 23:07  birdshome  阅读(2588)  评论(3编辑  收藏  举报

导航