当前位置: 首页 > news >正文

简单升级了一下可着色的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.gif{
InBlock.gif     if ( m_IsClear )
ExpandedSubBlockStart.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.gif    {
InBlock.gif         if ( alstStyle !=  null )
ExpandedSubBlockStart.gif        {
InBlock.gif            alstStyle.Clear();
ExpandedSubBlockEnd.gif        }
InBlock.gif         foreachstring key  in  this.Items[i].Attributes.CssStyle.Keys )
ExpandedSubBlockStart.gif        {
InBlock.gif             if ( htItemStyles ==  null )
ExpandedSubBlockStart.gif            {
InBlock.gif                htItemStyles =  new Hashtable();
ExpandedSubBlockEnd.gif            }
InBlock.gif             if ( m_ItemStyles !=  null )
ExpandedSubBlockStart.gif            {
InBlock.gif                 foreachobject obj  in m_ItemStyles )
ExpandedSubBlockStart.gif                {
InBlock.gif                     object [] objs = ( object [])obj;
InBlock.gif                    htItemStyles[objs[0]] = objs[1];
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif             if ( alstStyle ==  null )
ExpandedSubBlockStart.gif            {
InBlock.gif                alstStyle =  new ArrayList();
ExpandedSubBlockEnd.gif            }
InBlock.gif             string strKey = key.ToLower();
InBlock.gif             if ( strKey == "color" || strKey == "background-color" )
ExpandedSubBlockStart.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.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.gif    {
InBlock.gif         int count = 0;
InBlock.gif        m_ItemStyles =  new  object[htItemStyles.Count];
InBlock.gif         foreachobject key  in htItemStyles.Keys )
ExpandedSubBlockStart.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 附ColorableListBox控件源码

本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

相关文章:

  • jsoup入门
  • ospf和rip和静态路由三者的区别?各能实现什么功能?
  • 【译】ASP.NET Identity Core 从零开始
  • 工作中用到的linux、SQL、Python语句
  • php 几个算法
  • 2018年武汉大学653数学分析
  • iOS 中表格按时间戳分组排序
  • Java设计模式23种(搞笑版) (转)
  • Mac node js环境的安装与测试
  • Java并发编程实践读书笔记(2)多线程基础组件
  • HDFS High Availability Using the Quorum Journal Manager
  • 浏览器的渲染:过程与原理
  • StructureMap
  • linux的PHP扩展模块安装
  • hadoop3.0.0测验
  • 收藏网友的 源程序下载网
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • avalon2.2的VM生成过程
  • DOM的那些事
  • ES10 特性的完整指南
  • gitlab-ci配置详解(一)
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • learning koa2.x
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • v-if和v-for连用出现的问题
  • 给Prometheus造假数据的方法
  • 你真的知道 == 和 equals 的区别吗?
  • 爬虫模拟登陆 SegmentFault
  • 如何设计一个比特币钱包服务
  • 树莓派 - 使用须知
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 《码出高效》学习笔记与书中错误记录
  • 2017年360最后一道编程题
  • puppet连载22:define用法
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • !$boo在php中什么意思,php前戏
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (BFS)hdoj2377-Bus Pass
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • .mysql secret在哪_MySQL如何使用索引
  • .NET Framework .NET Core与 .NET 的区别
  • .Net Memory Profiler的使用举例
  • .NET业务框架的构建
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • /bin、/sbin、/usr/bin、/usr/sbin
  • @Autowired 与@Resource的区别