posts - 6, comments - 42, trackbacks - 0, articles - 1

导航

公告

用table写过了个类似DATALIST的水平重复显示的东东,要用的来看看吧。

Posted on 2006-09-25 23:34 小象 阅读(...) 评论(...) 编辑 收藏

最近有朋友和我要用table自己写过了个类似DATALIST的水平重复显示的东东,就写了个。不过时间参促也没重构,有兴趣的朋友可以来看看.欢迎有兴趣的朋友再重构。记得发我哦!

先定义基类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

/// <summary>
/// 小象的博客,wzjingwei.cnblogs.com
/// </summary>

namespace wzjingwei.cnblogs.com
{
    
public abstract class HorizontalRepeatBase:System .Web .UI .Page
    
{
        
        
protected ArrayList TCellCollection = new ArrayList();
        
protected ArrayList TRowCollection = new ArrayList();

        
public HorizontalRepeatBase()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
protected void ProduceHorizontalRepeatTable(int HorizontalRepeatCounts)
        
{
            
            
int nCells = TCellCollection.Count;//td数目


            
int nRows = 0;
            
if (Math.IEEERemainder((double)nCells, (double)HorizontalRepeatCounts) == 0)//判断是否整除
            {
                nRows 
= nCells / HorizontalRepeatCounts;
            }

            
else
            
{
                nRows 
= nCells / HorizontalRepeatCounts + 1;
            }


            
// nRows = nCells / HorizontalRepeatCounts;//最后生成的行数
            bool breakFlag = false;
            
for (int i = 0; i < nRows; i++)
            
{
                TableRow Row 
= new TableRow();

                
for (int j = 0; j < HorizontalRepeatCounts; j++)
                
{

                    Row.Cells.Add((TableCell)TCellCollection[i 
* HorizontalRepeatCounts + j]);
                    
if (i * HorizontalRepeatCounts + j == nCells - 1)
                    
{
                        breakFlag 
= true;
                        
break;
                    }

                }


                TRowCollection.Add(Row);

                
if (breakFlag)
                
break; }
            }



        }


        
abstract protected void ProduceTD();



    }



}
实现类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace wzjingwei.cnblogs.com
{
    
/// <summary>
    
/// 小象的博客,wzjingwei.cnblogs.com
    
/// </summary>

    public class HorizontalRepeat : HorizontalRepeatBase
    
{
        IDataReader dr;
        
int HorizontalRepeatCounts;
        Table MyTable;
        
string Category;


        
public HorizontalRepeat(IDataReader dr1, int HorizontalRepeatCounts1, ref Table table,string CategoryPara)
        
{
            dr 
= dr1;
            HorizontalRepeatCounts 
= HorizontalRepeatCounts1;
            MyTable 
= table;
            Category 
= CategoryPara;
        }


        
override protected void ProduceTD()
        
{
            
while (dr.Read())
            
{
                TableCell Cell 
= new TableCell();
                
double width = 1 / (double)HorizontalRepeatCounts * 100;
                Cell.Width 
= Unit.Percentage(width);
                
switch (Category)
                
{
                    
case "GetView":
                        GetViewMost(
ref Cell);
                        
break;
                    
                    
default:

                        
break;
                }

                TCellCollection.Add(Cell);
            }

        }


        
public void Do()
        
{
            ProduceTD();

            
base.ProduceHorizontalRepeatTable(HorizontalRepeatCounts);

            
for (int i = 0; i < TRowCollection.Count; i++)
            
{
                MyTable.Rows.Add((TableRow)TRowCollection[i]);
            }

        }




        
private void GetView(ref TableCell Cell)
        
{
            Cell.Text 
= "<div align='center'><a href=UserList.aspx?ProvinceType=All&Province=" +Server.UrlEncode ( dr["BaseInfoProvince"].ToString()) + ">" + dr["BaseInfoProvince"].ToString() + "(" + dr["ProvinceUserNum"].ToString() + ")</a> </div>";
                Cell.BorderWidth 
= Unit.Pixel(1);
        }


    }

}

        //调用方法
  IDataReader  dr = null;
        //被关注最多的6个
        DataAccessLayer.MainDataAccess MDA = new DataAccessLayer.MainDataAccess();
        //获得数据
  dr=MDA.GetView("6");
  //生成水平重复表格,下面参数中的6就是水平重复6次的意思。
        wzjingwei.cnblogs.com.HorizontalRepeat hrClass = new wzjingwei.cnblogs.com.HorizontalRepeat(dr, 6, ref this.ClassTable, "GetView");
        hrClass.Do();
dr.Close();