用户单击删除按钮时如何删除动态控件(文本框和按钮)
Posted
技术标签:
【中文标题】用户单击删除按钮时如何删除动态控件(文本框和按钮)【英文标题】:how to delete dynamic control(textbox and button) when user click delete button 【发布时间】:2010-12-25 20:07:05 【问题描述】:这是我的代码,我想知道当用户单击删除按钮时如何添加一些代码来删除动态控件(文本框和按钮)。请帮帮我T_T
使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.UI; 使用 System.Web.UI.WebControls; 使用 System.Globalization;
公共部分类_默认:System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (IsPostBack)
CreateTextBox();
else
Session["arrayTextBox"] = null;
Session["arrayButton"] = null;
protected void CreateTextBox()
List<TextBox> arrayTextBox = new List<TextBox>();
List<Button> arrayButton = new List<Button>();
if (TextBox1.Text != "")
if (Session["arrayTextBox"] != null)
arrayTextBox = (List<TextBox>)Session["arrayTextBox"];
arrayButton = (List<Button>)Session["arrayButton"];
TextBox aTextBox = new TextBox();
aTextBox.ID = "aTextBox" + arrayTextBox.Count.ToString();
aTextBox.Text = TextBox1.Text;
Button ButtonDelete = new Button();
ButtonDelete.ID = "ButtonDelete" + arrayButton.Count.ToString();
ButtonDelete.Text = "Delete";
//if (TextBox2.Text != "")
//
// String red = TextBox2.Text.ToString().Substring(0, 2);
// String green = TextBox2.Text.ToString().Substring(2, 2);
// String blue = TextBox2.Text.ToString().Substring(4, 2);
// int r = int.Parse(red, NumberStyles.AllowHexSpecifier);
// int g = int.Parse(green, NumberStyles.AllowHexSpecifier);
// int b = int.Parse(blue, NumberStyles.AllowHexSpecifier);
// aTextBox.BackColor = System.Drawing.Color.FromArgb(r, g, b);
//
arrayTextBox.Add(aTextBox);
arrayButton.Add(ButtonDelete);
for(int i=0;i<arrayTextBox.Count;i++)
PlaceHolder1.Controls.Add(arrayTextBox[i]);
PlaceHolder1.Controls.Add(arrayButton[i]);
PlaceHolder1.Controls.Add(new LiteralControl(@"<br />"));
//ButtonDelete.Click += new EventHandler(ButtonDelete_Click);
Session["arrayTextBox"] = arrayTextBox;
Session["arrayButton"] = arrayButton;
TextBox1.Text = "";
//TextBox2.Text = "";
【问题讨论】:
【参考方案1】:foreach arrayTextBox 并获取 TextBox,然后使用 PlaceHolder1.Controls.Remove(Textbox)
【讨论】:
【参考方案2】:您可以使用下面的代码。 注意:我使用按钮的CommandArgument来存储对应文本框的id,然后在btn点击事件中使用Page.Controls.Remove:
protected void Page_Load(object sender, EventArgs e)
if (IsPostBack)
CreateTextBox();
else
Session["arrayTextBox"] = null;
Session["arrayButton"] = null;
protected void ButtonDelete_Click(object Sender, EventArgs e)
Button btn = Sender as Button;
if(btn != null)
String szTextControlID = btn.CommandArgument;
Control ctl = Page.FindControl(szTextControlID);
Page.Controls.Remove(ctl);
Page.Controls.Remove(btn);
protected void CreateTextBox()
List<TextBox> arrayTextBox = new List<TextBox>();
List<Button> arrayButton = new List<Button>();
if (TextBox1.Text != "")
if (Session["arrayTextBox"] != null)
arrayTextBox = (List<TextBox>)Session["arrayTextBox"];
arrayButton = (List<Button>)Session["arrayButton"];
TextBox aTextBox = new TextBox();
aTextBox.ID = "aTextBox" + arrayTextBox.Count.ToString();
aTextBox.Text = TextBox1.Text;
Button ButtonDelete = new Button();
ButtonDelete.ID = "ButtonDelete" + arrayButton.Count.ToString();
ButtonDelete.CommandArgument = aTextBox.ID;
ButtonDelete.Text = "Delete";
ButtonDelete.Click += new EventHandler(ButtonDelete_Click);
//if (TextBox2.Text != "")
//
// String red = TextBox2.Text.ToString().Substring(0, 2);
// String green = TextBox2.Text.ToString().Substring(2, 2);
// String blue = TextBox2.Text.ToString().Substring(4, 2);
// int r = int.Parse(red, NumberStyles.AllowHexSpecifier);
// int g = int.Parse(green, NumberStyles.AllowHexSpecifier);
// int b = int.Parse(blue, NumberStyles.AllowHexSpecifier);
// aTextBox.BackColor = System.Drawing.Color.FromArgb(r, g, b);
//
arrayTextBox.Add(aTextBox);
arrayButton.Add(ButtonDelete);
for(int i=0;i<arrayTextBox.Count;i++)
PlaceHolder1.Controls.Add(arrayTextBox[i]);
PlaceHolder1.Controls.Add(arrayButton[i]);
PlaceHolder1.Controls.Add(new LiteralControl(@"<br />"));
Session["arrayTextBox"] = arrayTextBox;
Session["arrayButton"] = arrayButton;
TextBox1.Text = "";
//TextBox2.Text = "";
【讨论】:
以上是关于用户单击删除按钮时如何删除动态控件(文本框和按钮)的主要内容,如果未能解决你的问题,请参考以下文章
编写程序界面中包括一个标签、一个文本框和一个按钮。当用户单击按钮时,程序把文本框的内容复制到标签中