栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

为RadioButtonList每个项目设置ForeColor

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为RadioButtonList每个项目设置ForeColor

首先对比效果:

 

 

样式前RadioButtonList每个项目是没有设置前景色的。现在Insus.NET改写为样式后的效果。

这个改动,都是因昨晚开发时,儿子做完作业,来到Insus.NET身旁看了屏幕上写的程序。他用手指指并问,那是什么?Insus.NET跟他说“那是让用户可以选择颜色的功能。”他听了,回答说“既然是选择颜色的,怎会每个颜色都是黑色呢?”......

 

现在Insus.NET分享实现的方法,其中有应用了泛型(System.Collections.Generic)。

.aspx:

 

 

准备数据源,是一个泛型数据集:

View Code  private List Colours
    {
        get
        {
            List o = new List();
            o.Add("Red");
            o.Add("Blue");
            o.Add("Green");
            o.Add("SkyBlue");
            o.Add("LimeGreen");
            return o;
        }
    }

 

把数据绑定至RadioButtonList控件上:

View Code protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Data_Binding();
        }
    }

    private void Data_Binding()
    {
        this.RadioButtonListColour.DataSource = Colours.Select(c => new { value = c }).ToList();
        this.RadioButtonListColour.DataTextField = "value";
        this.RadioButtonListColour.DataBind();
    }

 

实现onDataBound="RadioButtonListColour_DataBound"事件:

View Code  protected void RadioButtonListColour_DataBound(object sender, EventArgs e)
    {
        var rbl = sender as RadioButtonList;
        foreach (ListItem li in rbl.Items)
        {
            Colours.ForEach(delegate(string s)
            {
                if (s == li.Text)
                {
                    li.Attributes.Add("style", "color:" + s);
                }
            });
        }
    }

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/247020.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号