如何动态的连续添加用户控件
一个button,每点一下,在table里多加一个用户控件,之前在用户控件里填的值和选择的内容不能掉。。。--------------------编程问答-------------------- 参考以下代码.原理是一样的.
private void Page_Load(object sender, System.EventArgs e)--------------------编程问答-------------------- protected void Page_Load(object sender, EventArgs e)
{
this.CreateTableHead();
for(int i=0;i<this.NewRowCount;i++)
{
this.AddRow();
}
}
private void CreateTableHead()
{
Table1.CellPadding = 0;
Table1.CellSpacing = 0;
Table1.Width = Unit.Percentage(100);
TableRow trHead = new TableRow();
TableCell tcHeadFoodName = new TableCell();
TableCell tcHeadType = new TableCell();
TableCell tcHeadNum = new TableCell();
tcHeadFoodName.Text = "食品名称";
tcHeadType.Text = "规格";
tcHeadNum.Text = "数量";
trHead.Cells.Add(tcHeadFoodName);
trHead.Cells.Add(tcHeadType);
trHead.Cells.Add(tcHeadNum);
trHead.HorizontalAlign = HorizontalAlign.Center;
trHead.BackColor = Color.AliceBlue;
trHead.Height = 30;
Table1.Rows.Add(trHead);
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();
TextBox tb1 = new TextBox();
tb1.ID = "tbFoodName" + Table1.Rows.Count;
TextBox tb2 = new TextBox();
tb2.ID = "tbType" + Table1.Rows.Count;
TextBox tb3 = new TextBox();
tb3.ID = "tbNum" + Table1.Rows.Count;
tc1.Controls.Add(tb1);
tc2.Controls.Add(tb2);
tc3.Controls.Add(tb3);
tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
tr1.Cells.Add(tc3);
tr1.HorizontalAlign = HorizontalAlign.Center;
tr1.Height = 30;
tr1.VerticalAlign = VerticalAlign.Middle;
Table1.Rows.Add(tr1);
}
private void AddRow()
{
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();
TextBox tb1 = new TextBox();
tb1.ID = "tbFoodName" + Table1.Rows.Count;
TextBox tb2 = new TextBox();
tb2.ID = "tbType" + Table1.Rows.Count;
TextBox tb3 = new TextBox();
tb2.ID = "tbNum" + Table1.Rows.Count;
tc1.Controls.Add(tb1);
tc2.Controls.Add(tb2);
tc3.Controls.Add(tb3);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);
Table1.Rows.Add(tr);
}
private int NewRowCount
{
get
{
return ViewState["RowCount"] == null?0:(int)ViewState["RowCount"];
}
set
{
ViewState["RowCount"] = value;
}
}
private void btnNewRow_Click(object sender, System.EventArgs e)
{
this.NewRowCount ++;
this.AddRow();
}
{
if (!IsPostBack)
{
ViewState["ctlNum"] = 0;
}
else
{
for (int counter = 0; counter < (int)ViewState["ctlNum"]; counter++)
{
AddControls();
}
}
}
protected void BtnAnd_Click(object sender, EventArgs e)
{
ViewState["ctlNum"] = (int)ViewState["ctlNum"] + 1;
AddControls();
}
private void AddControls()
{
CusBox webUserCtrl = (CusBox)Page.LoadControl("~/Control/CusBox.ascx");
ph.Controls.Add(webUserCtrl);
setControlsID();
}
private void setControlsID()
{
int i = 1;
foreach (CusBox wc in this.ph.Controls)
{
wc.ID = i.ToString();
i++;
}
} --------------------编程问答-------------------- 1楼那种代码怎么贴出来的啊
我怎么粘贴完了就啥格式都没了 --------------------编程问答-------------------- 那删除呢? --------------------编程问答-------------------- 删除按钮在用户控件里的,remove当前用户控件
补充:.NET技术 , ASP.NET