zoukankan      html  css  js  c++  java
  • .NET中动态生成的表格,当鼠标经过tr时改变tr当前颜色,当鼠标移开恢复原来的颜色

    首先看看效果一:

     

    效果二:

     

     实现上面效果,Insus.NET 是使用下面例子进行修改:

     http://www.cnblogs.com/insus/archive/2012/10/29/2744721.html

     效果一变动代码如下:

    View Code
     //for each List<T> 
            Pager objPg = new Pager();
            objPg.PagerCollections().ForEach(delegate(Pager p)
            {
                TableRow tbr = new TableRow();
                tbr.ID = "tr" + p.ID.ToString();

                //设置背景颜色            
                tbr.BackColor = System.Drawing.Color.Green;

                //宣告一个变量,当mouse over or out样式
                string style = "this.style.backgroundColor='@BackColor'";

                //宣告一个变量,将存储行原来颜色。
                string bgColor = string.Empty;

                TableCell tc0 = new TableCell();
                tc0.Text = p.ID.ToString();
                tc0.BorderWidth = Unit.Pixel(1);
                tbr.Cells.Add(tc0);

                TableCell tc1 = new TableCell();
                tc1.Text = p.Size;
                tc1.BorderWidth = Unit.Pixel(1);
                tbr.Cells.Add(tc1);

                //获取行原来颜色。
                bgColor = tbr.BackColor.Name;

                //设置mouse over事件, 最后一个参数"blue"是mouse over时的颜色,你可以自行指定。
                tbr.Attributes.Add("onmouseover", style.Replace("@BackColor""blue"));

                //设置mouse out事件
                tbr.Attributes.Add("onmouseout", style.Replace("@BackColor", bgColor));

                oTable.Rows.Add(tbr);
            });

    效果二变动代码如下: 

    View Code
     //for each List<T> 
            Pager objPg = new Pager();
            objPg.PagerCollections().ForEach(delegate(Pager p)
            {
                TableRow tbr = new TableRow();
                tbr.ID = "tr" + p.ID.ToString();

                //设置背景颜色
                if (p.ID % 2 == 0)
                    tbr.BackColor = System.Drawing.Color.Green;
                else
                    tbr.BackColor = System.Drawing.Color.Silver;

                //宣告一个变量,当mouse over or out样式
                string style = "this.style.backgroundColor='@BackColor'";

                //宣告一个变量,将存储行原来颜色。
                string bgColor = string.Empty;

                TableCell tc0 = new TableCell();
                tc0.Text = p.ID.ToString();
                tc0.BorderWidth = Unit.Pixel(1);
                tbr.Cells.Add(tc0);

                TableCell tc1 = new TableCell();
                tc1.Text = p.Size;
                tc1.BorderWidth = Unit.Pixel(1);
                tbr.Cells.Add(tc1);

                //获取行原来颜色。
                bgColor = tbr.BackColor.Name;

                //设置mouse over事件, 最后一个参数"blue"是mouse over时的颜色,你可以自行指定。
                tbr.Attributes.Add("onmouseover", style.Replace("@BackColor""blue"));

                //设置mouse out事件
                tbr.Attributes.Add("onmouseout", style.Replace("@BackColor", bgColor));

                oTable.Rows.Add(tbr);
            });
  • 相关阅读:
    Mvvm combobox绑定Dictionary问题
    类型转化方法(处理System.Nullable类型)
    linq 动态查询
    VS 2005 / 2008 / 2010 能否继续使用 ASP.NET 1.x版的DataGrid ????
    使用 Using...End Using区块来写程序,要非常小心!
    [习题]TreeView、Menu、SiteMapPath #2 多国语系 /当地语系 / Localization
    Repeater,不用自己写循环 (Loop)
    [习题]给初学者的范例,多重字段搜寻引擎 for GridView,兼论 SqlDataSource与SelectParameter的用法
    ASP.NET案例精编(清华大学出版社 / 作者MIS2000Lab)「勘误表」、补充习题与档案下载
    Windows Vista / 7减少不必要的服务、最佳化(优化)
  • 原文地址:https://www.cnblogs.com/insus/p/2744769.html
Copyright © 2011-2022 走看看