导航

Table翻页的实现

Posted on 2004-07-06 01:44  ChenRui  阅读(4266)  评论(0编辑  收藏  举报

晚上看到自己前几天写的“生成动态表格”,觉得太不专业了。在codeproject上看到很多分页的datagrid,自己也想写一个。不过是用table写的。
设计大致是这样的:输入行数,列数生成表格,可以翻页,跳转到某一页。比较简单。

Generate the Table

page

     
        Input the number of rows: 
        
Input the number of columns:

这是生成表格按钮的代码:

    
public int n_Page;
    
public static int k = 1;
    
    
void gen_Click(object sender, EventArgs e)
    
{
        
//Define the number of rows and columns
        k = 1;
        
int n_rows = 0;
        
int n_cols = 0;
        
//Exception handle
        if (row.Text==""|| cell.Text=="")
        
{
            message.InnerHtml 
= "<center><b><font color=red>please enter the right number</font></b></center>";
            
return;
        }

        
//Get the numbers of rows and columns
        n_rows = Convert.ToInt32(row.Text);
        n_cols 
= Convert.ToInt32(cell.Text);
        
//Prevent running out
        if (n_rows > 50 || n_rows < 0 || n_cols > 8 || n_cols < 0)
        
{
            message.InnerHtml 
= "<center><b><font color=red>the rows or the columns is too large or too small!</font></b></center>";
            
return;
        }

        
//Calculate the number of page
        message.InnerHtml = "";
        
if (n_rows % 5 != 0)
            n_Page 
= n_rows / 5 + 1;
        
else
            n_Page 
= n_rows / 5;
    
        message.InnerHtml 
= "<p><center>the page number is " + n_Page.ToString();
        nPage.Items.Clear();
    
        
//Generate the items of the dropdownlist
        for (int i = 0; i < n_Page; i++)
            nPage.Items.Add((i 
+ 1).ToString());
    
        
//Generate the Table
        int currentRow = 0;
        
for (int j = 0; j < min(5, n_rows); j++)
        
{
            HtmlTableRow r 
= new HtmlTableRow();
            
if (currentRow % 2 == 0)
                r.BgColor 
= "skyblue";
            
else
                r.BgColor 
= "yellow";
            currentRow
++;
            
for (int i = 0; i < n_cols; i++)
            
{
                HtmlTableCell c 
= new HtmlTableCell();
                c.InnerText 
= "Row " + (j + 1).ToString() + " Col " + (i + 1).ToString();
                r.Cells.Add(c);
            }

            table1.Rows.Add(r);
        }

    }

    

其他的按钮代码跟它大同小异,可以简单地copy移植,篇幅所限,就不贴了。关键是静态变量k。