C# Table服务器控件动态添加行疑问
页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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>无标题页</title>
</head>
<body>
<form id="form1" runat="server" enableviewstate="True">
<asp:Table ID="tbl_1" runat="server" Width="308px" style="border:1px solid green">
<asp:TableRow>
<asp:TableCell>国家</asp:TableCell>
<asp:TableCell>编码</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>中国</asp:TableCell>
<asp:TableCell>086</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:TextBox ID="tbx_1" runat="server" Width="154px"></asp:TextBox>
<asp:TextBox ID="tbx_2" runat="server" Width="152px"></asp:TextBox>
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Add" />
<br />
<br />
</form>
</body>
</html>
====================================================
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
Table mytable = new Table();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
TableCell tc1 = new TableCell();
TableRow mytr = new TableRow();
tc1.Text = tbx_1.Text.ToString();
TableCell tc2 = new TableCell();
mytr.Cells.Add(tc1);
tc2.Text = tbx_2.Text.ToString();
mytr.Cells.Add(tc2);
if (Session["s_table"] != null)
{
Table mytable = (Table)Session["s_table"];
mytable.Rows.Add(mytr);
Session["s_table"] = mytable;
}
else
{
Table tmptable = new Table();
tmptable.Rows.Add(mytr);
Session["s_table"] = tmptable;
}
Table stb = (Table)Session["s_table"];
foreach (TableRow tmptr in stb.Rows)
{
tbl_1.Rows.Add(tmptr);
}
}
}
}
===========================================
想实现的功能:
在文本框中输入要添加的数据,单击"ADD"按钮将数据添加到表中。
===========================================
问题点:
添加第一次成功,第1+N次将会覆盖前次添加的数据,并不会多产生一行。
===========================================
说明:
不想用JS代码来实现。
以上请高手赐教,谢谢~