今天一朋友在询问动态添加与删除控件的问题,编辑根据自己的经验,新建了一个动态往Panel控件中添加与删除Button按钮的示例,仅供参考:
首先在新建一个WebForm文件,在aspx文件中添加以下控件,以做测试之用,如下代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" % >
<!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:Panel ID="Panel1" runat="server" >
</asp:Panel >
<br / >
<asp:Button ID="Button1" runat="server" Text="增加" OnClick="Button1_Click" / >
<asp:Button ID="Button2" runat="server" Text="从头删除" OnClick="Button2_Click" / >
<asp:Button ID="Button3" runat="server" Text="从尾删除" OnClick="Button3_Click" / >
指定ID:<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox >
<asp:Button ID="Button4" runat="server" onclick="Button4_Click"
Text="删除指定ID的按钮" / >
</div >
</form >
</body >
</html >
一、添加方法:
点击Button1按钮往Panel中添加一个Button按钮:
protected void Button1_Click(object sender, EventArgs e)
{
Button li = new Button();
li.ID = "abc";
li.Text = "按钮";
this.Panel1.Controls.Add(li);
}
这时候我们发现,当我们第二次点击Button1时,前面添加的Button消失了。这时我们就可以想到,可以使用缓存将用户的每次点击所应该添加的按钮保存起来,于是添加了btnList属性:
public List<Button > btnList
{
get
{
if (Session["btnList"] == null)
{
Session["btnList"] = new List<Button >();
}
return Session["btnList"] as List<Button >;
}
}
同时添加将btnList属性中所有Button添加到Panel中的方法:
private void Data_DataBind()
{
foreach (Button li in btnList)
{
this.Panel1.Controls.Add(li);
}
}
修改Button1的Click事件如下:
protected void Button1_Click(object sender, EventArgs e)
{
Button li = new Button();
li.ID = "abc";
li.Text = "按钮";
btnList.Add(li);
Data_DataBind();
}
这样我们就可以持续往Panl中添加按钮了。
二、删除方法:
因为我们使用了btnList属性来缓存用户的Button集合,所以删除时,我们只需操作btnList属性即可,如下:
/// <summary >
/// 从头开始移除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button2_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(0);
}
Data_DataBind();
}
/// <summary >
/// 从尾开始删除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button3_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(btnList.Count - 1);
}
Data_DataBind();
}
/// <summary >
/// 删除指定ID的按钮
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button4_Click(object sender, EventArgs e)
{
string id = this.TextBox1.Text.Trim();
if (!string.IsNullOrEmpty(id))
{
Button btn = btnList.Find(delegate(Button btn_1) { return btn_1.ID == id; });
if (btn != null)
{
btnList.Remove(btn);
}
}
Data_DataBind();
}
.cs文件中的全部代码如下:
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
public List<Button > btnList
{
get
{
if (Session["btnList"] == null)
{
Session["btnList"] = new List<Button >();
}
return Session["btnList"] as List<Button >;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button li = new Button();
li.ID = "abc";
li.Text = "按钮";
btnList.Add(li);
Data_DataBind();
}
/// <summary >
/// 从头开始移除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button2_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(0);
}
Data_DataBind();
}
/// <summary >
/// 从尾开始删除
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button3_Click(object sender, EventArgs e)
{
if (btnList.Count > 0)
{
btnList.RemoveAt(btnList.Count - 1);
}
Data_DataBind();
}
/// <summary >
/// 删除指定ID的按钮
/// </summary >
/// <param name="sender" ></param >
/// <param name="e" ></param >
protected void Button4_Click(object sender, EventArgs e)
{
string id = this.TextBox1.Text.Trim();
if (!string.IsNullOrEmpty(id))
{
Button btn = btnList.Find(delegate(Button btn_1) { return btn_1.ID == id; });
if (btn != null)
{
btnList.Remove(btn);
}
}
Data_DataBind();
}
private void Data_DataBind()
{
foreach (Button li in btnList)
{
this.Panel1.Controls.Add(li);
}
}
}
}