62,052
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddContorl.aspx.cs" Inherits="AddContorl" %>
<!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>
<asp:Table ID="Table1" runat="server" BorderWidth="1px" Width="300px">
<asp:TableHeaderRow>
<asp:TableHeaderCell>序号</asp:TableHeaderCell>
<asp:TableHeaderCell>姓名</asp:TableHeaderCell>
<asp:TableHeaderCell>成绩</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
<asp:Button runat="server" Text="添加" ID="add" OnClick="add_Click" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="打印" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:Button ID="reduce" runat="server" OnClick="reduce_Click" Text="删除" /></div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class AddContorl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnInitComplete(EventArgs e)
{
base.OnInitComplete(e);
if (Session["rowNum"] == null || !Page.IsPostBack)
this.Session.Add("rowNum", 1);
this.initTable();
return;
}
protected void add_Click(object sender, EventArgs e)
{
Session["rowNum"] =1+ Convert.ToInt16(this.Session["rowNum"]);
int rowNum = Convert.ToInt16(this.Session["rowNum"]);
this.Label1.Text += rowNum;
TableRow row = new TableRow();
TableCell cell = new TableCell();
TextBox box = new TextBox();
box.ID = "num_" + rowNum;
cell.Controls.Add(box);
row.Cells.Add(cell);
cell = new TableCell();
box = new TextBox();
box.ID = "name_" + rowNum;
cell.Controls.Add(box);
row.Cells.Add(cell);
cell = new TableCell();
box = new TextBox();
box.ID = "score_" + rowNum;
cell.Controls.Add(box);
row.Cells.Add(cell);
this.Table1.Rows.Add(row);
}
public void initTable()
{
int num=Convert.ToInt16(this.Session["rowNum"]);
this.Label1.Text += " init" + num.ToString();
for(int i=1;i<=num;i++)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
TextBox box = new TextBox();
box.ID = "num_" + i.ToString();
cell.Controls.Add(box);
row.Cells.Add(cell);
cell = new TableCell();
box = new TextBox();
box.ID = "name_" + i.ToString();
cell.Controls.Add(box);
row.Cells.Add(cell);
cell = new TableCell();
box = new TextBox();
box.ID = "score_" + i.ToString();
cell.Controls.Add(box);
row.Cells.Add(cell);
this.Table1.Rows.Add(row);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int num = Convert.ToInt16(this.Session["rowNum"]);
string text = "";
text += this.Table1.Rows.Count;
/* for (int i = 1; i <= num; i++)
{
text += ((TextBox)Page.FindControl("num_" + i.ToString())).Text;
text += ((TextBox)Page.FindControl("name_" + i.ToString())).Text;
text += ((TextBox)Page.FindControl("score_" + i.ToString())).Text;
}
*
* */
this.Label1.Text = text;
}
protected void reduce_Click(object sender, EventArgs e)
{
int rowNum = Convert.ToInt16(this.Session["rowNum"]);
if (rowNum > 0)
{
rowNum--;
this.Session["rowNum"] = rowNum;
}
this.Table1.Rows.RemoveAt(rowNum + 1);
}
}
Panel p = new Panel();//页面容器。如果页面放置了Panel 且命名为Panel1 则 直接Panel1.Controls.Add(t);即可
TextBox t = new TextBox {ID = "t1", Text = "aaa"};
p.Controls.Add(t);