(急急急,大神们快来,在线坐等).net 点击Button动态生成TableCell 本帖最后由 yuandongze 于 2010-04-14 11:05:21 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你每次提交都刷新页面 页面又重新加载了次所以每次都只能加一个定义一个静态变量存储 TableCell tc = new TableCell();static List<TableCell> listc = new static List<TableCell>();点击 Button1_Click时先将 tc 加到 listc里之后再从 listc循环里读取添加到页面 点击button的时候引发page_load 你之前的生成的肯定没有了 可以考虑用js生成 这样做 生成是没有问题了, 但是点击另一个 Button想要 取值的时候,还是重新刷空,大哥在给想个办法吧 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.Collections.Generic;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;public partial class development : System.Web.UI.Page{ static List<TableCell> listc = new List<TableCell>(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["i"] = 0; } } protected void Button1_Click(object sender, EventArgs e) { TableCell tc=new TableCell(); tc.ID="tc" + ViewState["i"].ToString(); TextBox tx = new TextBox(); tx.ID = "tx" + ViewState["i"].ToString(); tx.Text = "Hello"; tc.Controls.Add(tx); listc.Add(tc); for (int j = 0; j < listc.Count; j++) { TableRow tr=new TableRow(); tr.ID="tr"+ViewState["i"].ToString(); tr.Cells.Add(listc[j]); t1.Controls.Add(tr); } //TableCell tc = new TableCell(); //tc.ID = "tc" + ViewState["i"].ToString(); //TextBox tx = new TextBox(); //tx.ID = "tx" + ViewState["i"].ToString(); //tx.Text = "Hello"; //tc.Controls.Add(tx); //tc.Width = Unit.Pixel(100); //TableRow tr = new TableRow(); //tr.ID = "tr" + ViewState["i"].ToString(); //tr.Controls.Add(tc); //t1.Rows.Add(tr); } protected void Button2_Click(object sender, EventArgs e) { for (int j = 0; j < Convert.ToInt32(ViewState["i"]); j++) { TextBox2.Text += (t1.FindControl("tx" + j) as TextBox).Text; } }} static List<TableCell> listc; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["i"] = 0; listc = new List<TableCell>(); } }这样试试 请教如何设置Session 【急】基于3层DataList嵌套,如何取得内层的控件 fck编辑器设置 有mssql dba吗?去哪认证?得具备什么样的条件? treeview TreeNode.NavigateUrl 怎样在一个WEB页面中调用一个EXE可执行程序?? 请问:一个很菜的问题! 文件上传时,扩展名为LOG的文件能上传,但下载时就会找不到路径 混淆器相关问题 问一个变量作用范围的问题 用FindControl在页面找控件的问题 WMV文件无法播放???在线等
定义一个静态变量存储 TableCell tc = new TableCell();
static List<TableCell> listc = new static List<TableCell>();
点击 Button1_Click时先将 tc 加到 listc里
之后再从 listc循环里读取添加到页面
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.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;public partial class development : System.Web.UI.Page
{
static List<TableCell> listc = new List<TableCell>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
ViewState["i"] = 0;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
TableCell tc=new TableCell();
tc.ID="tc" + ViewState["i"].ToString();
TextBox tx = new TextBox();
tx.ID = "tx" + ViewState["i"].ToString();
tx.Text = "Hello";
tc.Controls.Add(tx);
listc.Add(tc);
for (int j = 0; j < listc.Count; j++)
{
TableRow tr=new TableRow();
tr.ID="tr"+ViewState["i"].ToString();
tr.Cells.Add(listc[j]);
t1.Controls.Add(tr);
}
//TableCell tc = new TableCell();
//tc.ID = "tc" + ViewState["i"].ToString(); //TextBox tx = new TextBox();
//tx.ID = "tx" + ViewState["i"].ToString();
//tx.Text = "Hello";
//tc.Controls.Add(tx); //tc.Width = Unit.Pixel(100); //TableRow tr = new TableRow();
//tr.ID = "tr" + ViewState["i"].ToString();
//tr.Controls.Add(tc); //t1.Rows.Add(tr);
}
protected void Button2_Click(object sender, EventArgs e)
{
for (int j = 0; j < Convert.ToInt32(ViewState["i"]); j++)
{
TextBox2.Text += (t1.FindControl("tx" + j) as TextBox).Text;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
ViewState["i"] = 0;
listc = new List<TableCell>(); }
}
这样试试