none
c#中html表格的使用 RRS feed

答案

  • 你好

    我猜你是想知道asp.net HtmlTable的用法是吗?

    下面这个链接描述了他的一些基本属性以及描述。

    ASP.NET HtmlTable 控件

    下面的代码是个在ASP.NET中利用HtmlTable动态创建表格的例子,可以参考下。

    Deafault.aspx

    <%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
     <title>HtmlTable演示</title>
    </head>
    <body>
    <h1>HtmlTable实例演示</h1> 
     <form id="Form1" runat="server">
     <table id="MyTable" cellpadding="5" cellspacing="0" border="1" runat="server" >
     <tbody>
     </tbody>
     </table><p>
     输入表格行数:
     <input type="text" id="MyTextOne" value="1" runat="server"/></p>
     <p>输入表格单元格:
     <input type="text" id="MyTextTwo" value="1" runat="server" /></p>
     <p><input id="Submit1" type="submit" value="产生表格" runat="server" onserverclick="Submit1_ServerClick" /></p>
     </form>
    </body>
    </html>
     
    
    Deafault.aspx.cs
    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;
    public partial class _Default : System.Web.UI.Page
    {
     protected void Page_Load(object sender, EventArgs e)
     {
     }
     protected void Submit1_ServerClick(object sender, EventArgs e)
     {
      int row = 0;
      int numrows = Convert.ToInt32(MyTextOne.Value); // 获得文本框中整型数
      int numcells = Convert.ToInt32(MyTextTwo.Value);
      for (int j = 0; j < numrows; j++)
      {
      HtmlTableRow r = new HtmlTableRow();
      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); //添加该单元格对象
      }
      MyTable.Rows.Add(r);//添加行对
      }
      HtmlTableRow r2 = new HtmlTableRow();
      HtmlTableCell c2 = new HtmlTableCell();
      c2.Controls.Add(new LiteralControl("吉林大学远程教育学院"));
      c2.ColSpan = 3; //合并单元格,colspan属性可以实现单元格跨越多列
      r2.Cells.Add(c2);
      MyTable.Rows.Add(r2);
     }
    }
    希望对你有所帮助。

    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年3月9日 2:00

全部回复

  • 不知道您要做啥?希望您能多敲几个字,把问题描述出来。
    .NET类提供了很多的方法
    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年3月7日 8:39
  • 你好

    我猜你是想知道asp.net HtmlTable的用法是吗?

    下面这个链接描述了他的一些基本属性以及描述。

    ASP.NET HtmlTable 控件

    下面的代码是个在ASP.NET中利用HtmlTable动态创建表格的例子,可以参考下。

    Deafault.aspx

    <%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
     <title>HtmlTable演示</title>
    </head>
    <body>
    <h1>HtmlTable实例演示</h1> 
     <form id="Form1" runat="server">
     <table id="MyTable" cellpadding="5" cellspacing="0" border="1" runat="server" >
     <tbody>
     </tbody>
     </table><p>
     输入表格行数:
     <input type="text" id="MyTextOne" value="1" runat="server"/></p>
     <p>输入表格单元格:
     <input type="text" id="MyTextTwo" value="1" runat="server" /></p>
     <p><input id="Submit1" type="submit" value="产生表格" runat="server" onserverclick="Submit1_ServerClick" /></p>
     </form>
    </body>
    </html>
     
    
    Deafault.aspx.cs
    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;
    public partial class _Default : System.Web.UI.Page
    {
     protected void Page_Load(object sender, EventArgs e)
     {
     }
     protected void Submit1_ServerClick(object sender, EventArgs e)
     {
      int row = 0;
      int numrows = Convert.ToInt32(MyTextOne.Value); // 获得文本框中整型数
      int numcells = Convert.ToInt32(MyTextTwo.Value);
      for (int j = 0; j < numrows; j++)
      {
      HtmlTableRow r = new HtmlTableRow();
      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); //添加该单元格对象
      }
      MyTable.Rows.Add(r);//添加行对
      }
      HtmlTableRow r2 = new HtmlTableRow();
      HtmlTableCell c2 = new HtmlTableCell();
      c2.Controls.Add(new LiteralControl("吉林大学远程教育学院"));
      c2.ColSpan = 3; //合并单元格,colspan属性可以实现单元格跨越多列
      r2.Cells.Add(c2);
      MyTable.Rows.Add(r2);
     }
    }
    希望对你有所帮助。

    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年3月9日 2:00