通过js获取,而且效果好点。 var count=document.getElementById("table的id").rows.length;
前台 <table Id="NewTable" CellPadding="5" CellSpacing="0" Border="2" Runat="server"> <tbody> </tbody> </table> 后台 直接 引用 int rowcount = NewTable.Rows.Count;注意 Runat="server" using System; using System.Data; using System.Configuration; using System.Collections; 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 HTMLTAble_htmltable2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
HtmlTableRow objRow; HtmlTableCell objCell; int maxRows, maxCells; int c = 0; int r = 0; // 取得欄和儲存格數 maxRows = Convert.ToInt32(5); maxCells = Convert.ToInt32(6); // 建立表格 for (r = 0; r < 5; r++) { objRow = new HtmlTableRow(); if ((r / 2) != 0) { objRow.BgColor = "lightblue"; } for (c = 0; c < 6; c++) { objCell = new HtmlTableCell(); objCell.Controls.Add(new LiteralControl("列: " + r.ToString()+"<br/>" + " - 欄: " + c.ToString())); objRow.Cells.Add(objCell); } NewTable.Rows.Add(objRow); } } protected void Button1_Click(object sender, EventArgs e) { int rowcount = NewTable.Rows.Count; } } <%@ Page Language="C#" AutoEventWireup="true" CodeFile="htmltable2.aspx.cs" Inherits="HTMLTAble_htmltable2" %><!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>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <table Id="NewTable" CellPadding="5" CellSpacing="0" Border="2" Runat="server"> <tbody> </tbody> </table> </div> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> </form> </body> </html>
var count=document.getElementById("table的id").rows.length;
<table Id="NewTable" CellPadding="5" CellSpacing="0" Border="2" Runat="server">
<tbody>
</tbody>
</table> 后台 直接 引用
int rowcount = NewTable.Rows.Count;注意 Runat="server" using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 HTMLTAble_htmltable2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HtmlTableRow objRow;
HtmlTableCell objCell;
int maxRows, maxCells;
int c = 0;
int r = 0;
// 取得欄和儲存格數
maxRows = Convert.ToInt32(5);
maxCells = Convert.ToInt32(6);
// 建立表格
for (r = 0; r < 5; r++)
{
objRow = new HtmlTableRow(); if ((r / 2) != 0)
{
objRow.BgColor = "lightblue";
} for (c = 0; c < 6; c++)
{
objCell = new HtmlTableCell();
objCell.Controls.Add(new LiteralControl("列: " + r.ToString()+"<br/>" + " - 欄: " + c.ToString()));
objRow.Cells.Add(objCell); }
NewTable.Rows.Add(objRow); } }
protected void Button1_Click(object sender, EventArgs e)
{
int rowcount = NewTable.Rows.Count;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="htmltable2.aspx.cs" Inherits="HTMLTAble_htmltable2" %><!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table Id="NewTable" CellPadding="5" CellSpacing="0" Border="2" Runat="server">
<tbody>
</tbody>
</table>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
·详情请访问:http://www.515dns.com