急!!!!!!!!!!C#动态删除用户控件

kln359008500 2008-05-07 08:41:08
RT
...全文
1006 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
jimmy_jpy 2008-08-06
  • 打赏
  • 举报
回复
动态创建的控件,你在创建的时候就肯定赋予了name,那么在foreach的时候,根据那个name作对应的删除就可以了。
楼外楼 2008-05-07
  • 打赏
  • 举报
回复
找到控件父容器,从它的Controls里面Remove你要干掉的控件
优途科技 2008-05-07
  • 打赏
  • 举报
回复
foreach (Control c in panel2.Controls)
{
if (c is CheckBox)
{
//Control.Controls.Remove(removeControl);执行你的操作
}
}
nmd002 2008-05-07
  • 打赏
  • 举报
回复
从窗体里删除时,可以用Form.Controls得到全部控件对象,用For Each 循环一遍,按控件名称(.Name)来检查,找到想删除的那个,就使用1楼说过的Form.Controls.Remove(取得的控件对象)方法从窗体中删除既可.为了保证这个控件不会影响到内存的使用,最好把取得的控件用销毁的那个方法清除掉,名称忘记了,好像叫D什么什么的那个.
这样窗体中实际上就已经失去这个控件了.如果有时画面的显示还有残留,那是因为没刷新过,只要采用窗体画面的刷新方法Form.Refresh(),就会解决.
ismezy2002 2008-05-07
  • 打赏
  • 举报
回复
看得出你很急,连问题都来不及描述清楚。动态删除控件无非就是从容器的控件集合中移除,Control.Controls.Remove(removeControl);
caicai_45 2008-05-07
  • 打赏
  • 举报
回复
ISQL的定义可以简化为:


public interface ISQL
{
EventHandler DeleteSelf
{
get;
set;
}

}


整理过后的主页面的代码

Dictionary<Guid, string> controlNameList;//存放控件名称

protected void Page_Load(object sender, EventArgs e)
{

if (ViewState["UCNameList"] != null)
{
controlNameList = (Dictionary<Guid, string>)ViewState["UCNameList"];
}
else
{
controlNameList = new Dictionary<Guid, string>();
}
SetAllControl();


}

/// <summary>
/// 设置并加载所有增加上去的用户控件
/// </summary>
private void SetAllControl()
{
if (this.controlNameList != null)
{
foreach (Guid g in this.controlNameList.Keys)
{
string controlName = this.controlNameList[g].ToString();

LoadUCControl(controlName, g);
}
}
}

/// <summary>
/// 加载单个用户控件
/// </summary>
/// <param name="controlName">控件名称</param>
/// <param name="guid">控件ID</param>
private void LoadUCControl(string controlName, Guid guid)
{
if (controlName != string.Empty)
{
UserControl uc = (UserControl)Page.LoadControl("~/UserControl/" + controlName);
if (uc != null)
{
if (uc is ISQL)
{
((ISQL)uc).DeleteSelf += RemoveControl;
uc.ID = guid.ToString();

this.PHControl.Controls.Add(uc);
}
}
}

}

/// <summary>
/// 增加一个用户控件
/// </summary>
/// <param name="controlName"></param>
private void AddControl(string controlName)
{
if (controlName != string.Empty)
{
UserControl uc = (UserControl)Page.LoadControl("~/UserControl/" + controlName);
if (uc != null)
{
if (uc is ISQL)
{
//设置ViewState
Guid guid = Guid.NewGuid();
uc.ID = guid.ToString();

controlNameList.Add(guid, controlName);
ViewState["UCNameList"] = this.controlNameList;

//加载控件
LoadUCControl(controlName, guid);
}
}
}
}

/// <summary>
/// 删除用户控件
/// </summary>
/// <param name="send"></param>
/// <param name="e"></param>
private void RemoveControl(object send, EventArgs e)
{
string deleteID = ((UserControl)send).ID;
Guid guid = new Guid(deleteID);
this.controlNameList.Remove(guid);
this.PHControl.Controls.Remove(this.PHControl.FindControl(deleteID));
}



//用户控件


public partial class UserControl_UCOrder : System.Web.UI.UserControl, ISQL
{
public EventHandler deleteSelf;
protected void Page_Load(object sender, EventArgs e)
{
///
}
#region ISQL Members

public EventHandler DeleteSelf
{
get
{
return this.deleteSelf;
}
set
{
this.deleteSelf = value;
}
}

#endregion
protected void BtnDelete_Click(object sender, EventArgs e)
{
if (deleteSelf != null)
{
deleteSelf(this, e);
}
}
}

kln359008500 2008-05-07
  • 打赏
  • 举报
回复
谢谢!分不多,请笑纳!
caicai_45 2008-05-07
  • 打赏
  • 举报
回复
哎,估计我的代码你看了也没啥用。。。
里面有事件和委托。。
kln359008500 2008-05-07
  • 打赏
  • 举报
回复
我试试
caicai_45 2008-05-07
  • 打赏
  • 举报
回复
主页面的代码中有些是我自己的业务逻辑,自己斟酌。
kln359008500 2008-05-07
  • 打赏
  • 举报
回复
HaseTable已经不忘的忘完了,可以提供一下源代码吗?
caicai_45 2008-05-07
  • 打赏
  • 举报
回复
控件页面

public EventHandler deleteSelf;
PageLoad()
{
...
}
//页面上有个删除按钮,按钮事件
protected void BtnDelete_Click(object sender, EventArgs e)
{
if (deleteSelf != null)
{
deleteSelf(this, e);
}
}
caicai_45 2008-05-07
  • 打赏
  • 举报
回复
主页面

Dictionary<Guid, string> controlNameList;//存放控件名称
private string sql;//提取语句
private string condition;//条件
private string hand;//手工数据
protected void Page_Load(object sender, EventArgs e)
{

if (ViewState["UCNameList"] != null)
{
controlNameList = (Dictionary<Guid, string>)ViewState["UCNameList"];
}
else
{
controlNameList = new Dictionary<Guid, string>();
}
SetAllControl();


}

/// <summary>
/// 设置并加载所有增加上去的用户控件
/// </summary>
private void SetAllControl()
{
if (this.controlNameList != null)
{
foreach (Guid g in this.controlNameList.Keys)
{
string controlName = this.controlNameList[g].ToString();

LoadUCControl(controlName, g);
}
}
}

/// <summary>
/// 加载单个用户控件
/// </summary>
/// <param name="controlName">控件名称</param>
/// <param name="guid">控件ID</param>
private void LoadUCControl(string controlName, Guid guid)
{
if (controlName != string.Empty)
{
UserControl uc = (UserControl)Page.LoadControl("~/UserControl/" + controlName);
if (uc != null)
{
if (uc is ISQL)
{
((ISQL)uc).DeleteSelf += RemoveControl;
uc.ID = guid.ToString();

this.PHControl.Controls.Add(uc);
}
}
}

}

private void SaveAllViewState(string controlName)
{
if (controlName != string.Empty)
{
UserControl uc = (UserControl)Page.LoadControl("~/UserControl/" + controlName);
if (uc != null)
{
if (uc is ISQL)
{
//设置ViewState
Guid guid = Guid.NewGuid();
uc.ID = guid.ToString();

controlNameList.Add(guid, controlName);
ViewState["UCNameList"] = this.controlNameList;

//加载控件
LoadUCControl(controlName, guid);
}
}
}


}



private void RemoveControl(object send, EventArgs e)
{
string deleteID = ((UserControl)send).ID;
Guid guid = new Guid(deleteID);
this.controlNameList.Remove(guid);
this.PHControl.Controls.Remove(this.PHControl.FindControl(deleteID));

}
kln359008500 2008-05-07
  • 打赏
  • 举报
回复
有代码的话,需要,
caicai_45 2008-05-07
  • 打赏
  • 举报
回复
给个思路吧。动态添加的时候,所有控件的ID都用GUID,同时,有个HaseTable来记录到底增加那些控件,key为guid,name为控件的名称。
增加和删除的时候,都要把这张hashtable记录到ViewState或者Session中。
caicai_45 2008-05-07
  • 打赏
  • 举报
回复
说实话,这个东西太麻烦了。
增加的时候,点击按钮增加,删除的时候,点击用户控件上自己携带的删除按钮来删除自己。
必须要做到点一次,马上及时响应。

用户控件必须要申明一个public的EventHandler,这个事件绑定用户控件的自己的删除按钮的方法。

我这里有代码,但是比较麻烦一些,如果你要的话。
h_w_king 2008-05-07
  • 打赏
  • 举报
回复
一样的.
动态添加:
TextBox t = new TextBox();
t.Name = "aaaaa";
this.Controls.Add(t);


再动态删除
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
TextBox t = (TextBox)c;
if (t.Name == "aaaaa") this.Controls.Remove(c);
}
}
kln359008500 2008-05-07
  • 打赏
  • 举报
回复
只是要删除的那些控件是也是动态创建的,怎么获得所有控件呀?
ldy3881685 2008-05-07
  • 打赏
  • 举报
回复
先获取窗体所有控件,然后REMOVE
kln359008500 2008-05-07
  • 打赏
  • 举报
回复
有大侠给进来解决一下
加载更多回复(1)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧