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 Default28 : System.Web.UI.Page { int i; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Session["num"] = i; i = 0; Session["i"] = i; Response.Write("第一次:"+Session["i"].ToString()); } } protected void Button1_Click(object sender, EventArgs e) {
int j=Convert.ToInt32(Session["i"])+1; Session["i"] =Convert.ToString(j); Response.Write(Session["i"].ToString()); for (int i = 0; i < Convert.ToInt32(Session["i"]); i++) {
实际上你每次点击button都是新生成了一个textbox的,只是以前生成的不见了
HtmlTable tabletest= new HtmlTable ();//添加表格
HtmlTableRow trContent = new HtmlTableRow();//添加tr
HtmlTableCell tdtxt = new HtmlTableCell(); //添加td
TextBox txt1 = new TextBox();添加textbox
txt1.ID = "txt_01";
txt1.Attributes.Add("onfocus", "this.select();");
txt1.Text ="abc";
tdtxt.Controls.Add(txt1);
trContent.Cells.Add(tdtxt);
tabletest.Rows.Add(trContent);
//2009/12/29 测试通过
static int i = 0;
protected void Button1_Click(object sender, EventArgs e)
{
for (int j = 0; j <= i; j++)
{
TextBox tx = new TextBox();
tx.ID = "ID" + j.ToString();
this.PlaceHolder1.Controls.Add(tx);
}
i++;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
var i = 0;
function aa() {
i++;
txt = document.createElement("input");
txt.type = "text";
txt.id = "aa" + i;
txt.setAttribute("onclick", "aa()");
div1.appendChild(txt);
}
</script>
</head>
<body id="aaaa">
<form id="form1" runat="server">
<div style=" width:auto; vertical-align:middle;" id="div1">
<input id="Text1" type="text" onclick="aa();" />
</div>
</form>
</body>
</html>
然后在服务器端用Request.Form取服务器端进行添加控制,比较麻烦,每次都要记录现在应该创建几个TextBox
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 Default28 : System.Web.UI.Page
{ int i;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Session["num"] = i; i = 0;
Session["i"] = i;
Response.Write("第一次:"+Session["i"].ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int j=Convert.ToInt32(Session["i"])+1;
Session["i"] =Convert.ToString(j);
Response.Write(Session["i"].ToString());
for (int i = 0; i < Convert.ToInt32(Session["i"]); i++)
{
TextBox tx = new TextBox();
tx.ID = "ID" + i.ToString();
this.Form.Controls.Add(tx);
}
}
}
我试过了可以。你可以看看;