<html>
	<title>第十三例 HtmlTable控件的使用</title>
	<head>
		<script language="C#" runat="server">
       public int page=1;
              static int k=0;
       void Submit1(Object sender, EventArgs e) {
	
	  int m =0 ;   	  
          int row = 0;
	if (( Select1.Value == "" ) || ( Select2.Value == "" ) ) 
	{
	Message.InnerHtml="输入的行列值不能够为空";
	return;
	}
          //generate rows and cells
          int numrows = Convert.ToInt32(Select1.Value);
          int numcells = Convert.ToInt32(Select2.Value);
  	
	if ((numrows > 50 ) || (numrows < 0 ) || (numcells < 0) || (numcells > 8))
		{
		Message.InnerHtml="不能大于8列50行";
	      return;
		}
	  k=0;
  	  int mod1=0;

	  Message.InnerHtml="";
	  if (numrows % 6 != 0) mod1=1;
	  page=numrows / 6+mod1;	
	  Message1.InnerHtml="<p><P>按您的输入的数值共有页数 <font color=red size=4>" + Convert.ToString(page) + "</font> 页，转到第：";
	  for (;m < page;m++) 
		{
		StateSelect.Items.Add ((m+1).ToString());
		}
	 
          for (int j=0; j<min(6,numrows); j++) {
             HtmlTableRow r = new HtmlTableRow();
             // set bgcolor on alternating rows
             if (row % 2 == 1)
                r.BgColor="Gainsboro";
             row++;
             for (int i=0; i<numcells; i++) {
                HtmlTableCell c = new HtmlTableCell();
                c.Controls.Add(new LiteralControl("第" + (j+1).ToString() +"行"+
                               "，第" + (i+1).ToString()+"列"));
                r.Cells.Add(c);
             }
             Table1.Rows.Add(r);
          }
       }

       void Submit2(Object sender, EventArgs e) {
          int row = 0;
          int numrows = Convert.ToInt32(Select1.Value);
          int numcells = Convert.ToInt32(Select2.Value);
	  int mod1=0;

	  Message.InnerHtml="";
	  if (numrows%6 != 0) mod1=1;
	  page=numrows/6+mod1;
	if (k < page-1) k++;
	    for (int j=k*6; j<min((k+1)*6,numrows); j++) {
             HtmlTableRow r = new HtmlTableRow();
             // set bgcolor on alternating rows
             if (row%2 == 1)
                r.BgColor="Gainsboro";
             row++;
             for (int i=0; i<numcells; i++) {
                HtmlTableCell c = new HtmlTableCell();
                c.Controls.Add(new LiteralControl("第" + (j+1).ToString() +"行"+
                               "，第" + (i+1).ToString()+"列"));
                r.Cells.Add(c);
             }
             Table1.Rows.Add(r);
          }
       }
       void Submit3(Object sender, EventArgs e) {
          int row = 0;
          int numrows = Convert.ToInt32(Select1.Value);
          int numcells = Convert.ToInt32(Select2.Value);
	  int mod1=0;

	  Message.InnerHtml="";
	  if (numrows%6 != 0) mod1=1;
	  page=numrows/6+mod1;
          if (k > 0) k--;
          for (int j=k*6; j<min((k+1)*6,numrows); j++) {
             HtmlTableRow r = new HtmlTableRow();
             // set bgcolor on alternating rows
             if (row%2 == 1)
                r.BgColor="Gainsboro";
             row++;
             for (int i=0; i<numcells; i++) {
                HtmlTableCell c = new HtmlTableCell();
                c.Controls.Add(new LiteralControl("第" + (j+1).ToString() +"行"+
                               "，第" + (i+1).ToString()+"列"));
                r.Cells.Add(c);
             }
             Table1.Rows.Add(r);
          }
       }
       int min(int x,int y)
	{
       if (x>y) return y;
	else return x;
        }

        void Change1(Object sender, EventArgs e) 
	{ 
	  if (StateSelect.Value == "") 
     	    {
   	    Message.InnerHtml="请您首先输入行列值，然后生成表格";
	    return;
	     }
	  int k=Convert.ToInt32(StateSelect.Value)-1;
	  int row = 0;
          int numrows = Convert.ToInt32(Select1.Value);
          int numcells = Convert.ToInt32(Select2.Value);
	  int mod1=0;

	  Message.InnerHtml="";
	  if (numrows%6 != 0) mod1=1;
	  page=numrows/6+mod1;
          for (int j=k*6; j<min((k+1)*6,numrows); j++) {
             HtmlTableRow r = new HtmlTableRow();
             // set bgcolor on alternating rows
             if (row%2 == 1)
                r.BgColor="Gainsboro";
             row++;
             for (int i=0; i<numcells; i++) {
                HtmlTableCell c = new HtmlTableCell();
                c.Controls.Add(new LiteralControl("第" + (j+1).ToString() +"行"+
                               "，第" + (i+1).ToString()+"列"));
                r.Cells.Add(c);
             }
             Table1.Rows.Add(r);
          }
	}
    </script>
	</head>
	<body>
		<h3>
			<font face="Verdana">第十三例 HtmlTable控件的使用</font>
		</h3>
		<form runat="server">
			<font face="Verdana" size="-1">
				<p>
					<table id="Table1" CellPadding="5" CellSpacing="0" Border="1" BorderColor="black" runat="server" />
					<input id="Submit31" type="button" value="上一页" runat="server" Onserverclick="Submit3">
					<input id="Submit21" type="button" value="下一页" runat="server" Onserverclick="Submit2">
					<span id="Message1" runat="server" /><select id="StateSelect" runat="server" />页
					<input type="submit" value="提交" runat="server" Onserverclick="Change1">
				<p>
					请您输入表格行数: <input type="text" id="Select1" runat="server">
					<br>
					请您输入表格列数: <input type="text" id="Select2" runat="server">
					<BR>
					<input type="submit" value="生成表格" Onserverclick="Submit1" runat="server">
			</font>
		</form>
		<span id="Message" runat="server" />
	</body>
</html>
