我写了个例子.
有两个问题:
1.在服务器端如何获取某行某列的内容?
2.如果不用hidden,有可能让asp.net自己读入这个table内容吗?
在例子中table1的行数始终是2行,可以让table1的行数与实际行数相符吗?
请各位高手赐教!谢谢!
Default3.aspx
-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" validateRequest=false %>
<!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>
<script type="text/javascript">
function addRow(tableId) {
var domTable = document.getElementById(tableId);
var _row = domTable.insertRow();
var _col = _row.insertCell();
_col.innerHTML = "<input type='text' />";
}
function saveTable(tableId) {
var domTable = document.getElementById(tableId);
var hid = document.getElementById("Hidden1");
hid.value = domTable.innerHTML;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="table1" width="100%" runat="Server">
<tr>
<td>
<input type="text" />
</td>
</tr>
<tr>
<td>
<input type="text" />
</td>
</tr>
</table>
<input id="Button1" type="button" value="为table加一行" onclick="addRow('table1');" />
<asp:Button OnClientClick="saveTable('table1');" ID="Button2" runat="server" Text="提交到服务器" onclick="Button2_Click" />
<input id="Hidden1" type="hidden" runat="Server" />
</div>
</form>
</body>
</html>
-
Default3.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("table的行数为:" + table1.Rows.Count + "<br/>" + Hidden1.Value);
}
}
-