.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);
        });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM