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

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

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

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

None.gif protected   override   void  OnPreRender(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if ( m_IsClear )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        m_ItemStyles 
= null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    ArrayList alstStyle 
= null;
InBlock.gif    Hashtable htItemStyles 
= null;
InBlock.gif
InBlock.gif    
forint i=0 ; i < this.Items.Count ; ++i )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( alstStyle != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            alstStyle.Clear();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
foreachstring key in this.Items[i].Attributes.CssStyle.Keys )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( htItemStyles == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                htItemStyles 
= new Hashtable();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( m_ItemStyles != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreachobject obj in m_ItemStyles )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
object [] objs = (object [])obj;
InBlock.gif                    htItemStyles[objs[
0]] = objs[1];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ( alstStyle == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                alstStyle 
= new ArrayList();
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ( alstStyle != null && alstStyle.Count > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string [] strAry = new string[alstStyle.Count];
InBlock.gif            alstStyle.CopyTo(strAry);
InBlock.gif            htItemStyles[i] 
= strAry;
ExpandedSubBlockEnd.gif        }
            
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockEnd.gif    }

InBlock.gif    
base.OnPreRender(e);
ExpandedBlockEnd.gif}
None.gif

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

ContractedBlock.gif ExpandedBlockStart.gif 附ColorableListBox控件源码 #region 附ColorableListBox控件源码
InBlock.gif
using System;
InBlock.gif
using System.IO;
InBlock.gif
using System.Collections;
InBlock.gif
using System.Text;
InBlock.gif
using System.Text.RegularExpressions;
InBlock.gif
using System.Web.UI;
InBlock.gif
using System.Web.UI.WebControls;
InBlock.gif
using System.ComponentModel;
InBlock.gif
InBlock.gif
namespace WebExcel.UI.WebControls
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for ColorableListBox.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [DefaultProperty("Text")]
InBlock.gif    [ToolboxData(
"<{0}:ColorableListBox runat=server></{0}:ColorableListBox>")]
InBlock.gif    
public class ColorableListBox : ListBox
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private object [] m_ItemStyles;
InBlock.gif        
private bool m_IsClear = false;
InBlock.gif
InBlock.gif        
protected override object SaveViewState()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList alstViewState 
= new ArrayList();
InBlock.gif            
object objBaseVS = base.SaveViewState();
InBlock.gif            alstViewState.Add(objBaseVS);
InBlock.gif            alstViewState.Add(m_ItemStyles);
InBlock.gif            
return alstViewState;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void LoadViewState(object savedState)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( savedState != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ArrayList alstViewState 
= (ArrayList)savedState;
InBlock.gif                
if ( alstViewState.Count >= 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
base.LoadViewState(alstViewState[0]);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if ( alstViewState.Count == 2 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    m_ItemStyles 
= (object [])alstViewState[1];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
protected override void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( m_IsClear )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_ItemStyles 
= null;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            ArrayList alstStyle 
= null;
InBlock.gif            Hashtable htItemStyles 
= null;
InBlock.gif
InBlock.gif            
forint i=0 ; i < this.Items.Count ; ++i )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( alstStyle != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    alstStyle.Clear();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
foreachstring key in this.Items[i].Attributes.CssStyle.Keys )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if ( htItemStyles == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        htItemStyles 
= new Hashtable();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if ( m_ItemStyles != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
foreachobject obj in m_ItemStyles )
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
object [] objs = (object [])obj;
InBlock.gif                            htItemStyles[objs[
0]] = objs[1];
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if ( alstStyle == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        alstStyle 
= new ArrayList();
ExpandedSubBlockEnd.gif                    }

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

ExpandedSubBlockEnd.gif                }

InBlock.gif                
if ( alstStyle != null && alstStyle.Count > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string [] strAry = new string[alstStyle.Count];
InBlock.gif                    alstStyle.CopyTo(strAry);
InBlock.gif                    htItemStyles[i] 
= strAry;
ExpandedSubBlockEnd.gif                }
            
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.OnPreRender(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// clear custom item styles
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void ClearItemStyles()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_IsClear 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void DataBind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ClearItemStyles();
InBlock.gif            
base.DataBind ();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// Render this control to the output parameter specified.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="output"> The HTML writer to write out to </param>

InBlock.gif        protected override void Render(HtmlTextWriter output)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            StringBuilder strb 
= new StringBuilder();
InBlock.gif            StringWriter sw 
= new StringWriter(strb);
InBlock.gif            HtmlTextWriter htw 
= new HtmlTextWriter(sw);
InBlock.gif            
base.Render(htw);
InBlock.gif            
InBlock.gif            
if ( m_ItemStyles != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreachobject obj in m_ItemStyles )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
object [] objs = (object [])obj;
InBlock.gif                    
int i = (int)objs[0];
InBlock.gif                    AppendColorAttributes(strb, 
this.Items[i], (string [])objs[1]);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            output.Write(strb.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void AppendColorAttributes(StringBuilder select, ListItem li, string [] styles)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strItemPattern = @"value=""{0}""{1}>{2}</option>";
InBlock.gif            
string strOriginalItem = string.Format(strItemPattern, li.Value, "", li.Text);
InBlock.gif            
string strStyle = " style=\"";
InBlock.gif
            forint i=0 ; i < styles.Length ; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
string [] keyvalue = styles[i].Split(new char [] dot.gif{':'});
InBlock.gif                
string strStyleKey = keyvalue[0];
InBlock.gif                
string strStyleValue = keyvalue[1];
InBlock.gif                strStyle 
+= String.Format("{0}:{1};", strStyleKey.ToLower(), strStyleValue);
ExpandedSubBlockEnd.gif            }

InBlock.gif            strStyle 
+= @"""";
InBlock.gif            
string strNewItem = string.Format(strItemPattern, li.Value, strStyle, li.Text);
InBlock.gif            select.Replace(strOriginalItem, strNewItem);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
#endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值