删除动态用户控制
我有一个动态加载用户控件的网页。每个用户控件都可以在其内部递归加载相同的控件。每个控件都有一个删除按钮。我遇到的问题是,当用户单击删除按钮时,我需要删除包含删除按钮的控件。
页面控件集合可能如下所示:
Page
Group 1
Category Control
Category Control
Category Control
Category Control
Category Control
Category Control
如何删除新创建的类别控件。我已经尝试过这个。Dispose 但它不起作用。
在添加时动态加载用户控件的主页上,我有:
protected void cmdsubmitsofficecat_Click(object sender, EventArgs e)
{
int retvalue = 0;
SQLConnectivity db = new SQLConnectivity();
SqlParameter[] param = new SqlParameter[8];
List<group.category> flist = new List<group.category>();
param[0] = db.MakeInputParameter("@group_id", 1);
param[1] = db.MakeInputParameter("@parent_id", DBNull.Value);
param[2] = db.MakeInputParameter("@category_name", this.txtofficecat.Text.Trim());
param[3] = db.MakeInputParameter("@description", this.txtofficecat.Text.Trim());
param[4] = db.MakeInputParameter("@organization_id", 1);
param[5] = db.MakeInputParameter("@created_by", 1);
param[6] = db.MakeInputParameter("@is_active", 1);
param[7] = db.MakeOutputIntegerParameter("@category_id");
db.RunNonQueryProcedure("PerformanceCategorySave", param, ref retvalue);
if (retvalue != 0)
{
group.category item = new group.category();
item.catid = retvalue;
item.catname = this.txtofficecat.Text.Trim();
item.desc = this.txtofficecat.Text.Trim();
item.isactive = true;
item.parid = 0;
catctl ctl = (catctl)Page.LoadControl("~/catctl.ascx");
ctl.groupid = 1;
ctl.organizationid = 1;
ctl.categoryid = item.catid;
ctl.categoryname = item.catname;
ctl.parentid = item.parid;
ctl.ctltype = 1;
ctl.clist = item.categories;
ctl.plist = item.points;
this.officephld.Controls.Add(ctl);
this.switch_officefields(false, 1);
}
}
在 Web 用户控件上,我有一个 Page_Load 方法为:
protected void Page_Load(object sender, EventArgs e)
{
Label ctname = (Label)this.FindControl("ctl0_catname");
this.catname.Text = categoryname;
this.cmdaddcat.Attributes.Add("catid", categoryid.ToString());
this.cmddeletecat.ID = "cmddeletecat" + categoryid.ToString();
this.cmddeletecat.Click += new ImageClickEventHandler(this.cmddeletecat_Click);
if ( clist != null && clist.Count > 0 )
{
int i = 0;
this.cmddeletecat.Visible = false;
foreach ( group.category item in clist )
{
catctl ctl = (catctl)Page.LoadControl("~/catctl.ascx");
ctl.categoryid = item.catid;
ctl.categoryname = item.catname;
ctl.organizationid = organizationid;
ctl.groupid = groupid;
ctl.parentid = item.parid;
ctl.clist = item.categories;
ctl.plist = item.points;
ctl.ctltype = ctltype;
this.newphld.Controls.Add(ctl);
}
}
if ( plist != null && plist.Count > 0 )
{
this.rptPts.Visible = true;
this.rptPts.DataSource = plist;
this.rptPts.DataBind();
this.cmddeletecat.Visible = false;
}
this.Switch_Usability();
}
和一个删除方法为:
protected void cmddeletecat_Click(object sender, ImageClickEventArgs e)
{
List<group.category> flist = new List<group.category>();
SQLConnectivity db = new SQLConnectivity();
SqlParameter[] param = new SqlParameter[1];
DataTable dt = new DataTable();
DataTable sdt = new DataTable();
group parent = new group();
param[0] = db.MakeInputParameter("@category_id", categoryid);
db.RunNonQueryProcedure("PerformanceCategoryDelete", param);
if ( Page != null )
{
PlaceHolder ctl = (PlaceHolder)Page.FindControl("officephld");
if ( ctl != null )
{ ctl.Controls.Remove(this); }
}
}
I have a webpage that dynamically loads a user control. Each user control can recursively load the same control within itself. Each control has a delete button. The problem I am having is when the user clicks on the delete button, I need to remove the control that the delete button is contained in.
The page control collection can look like the following:
Page
Group 1
Category Control
Category Control
Category Control
Category Control
Category Control
Category Control
How do I remove a newly created Category Control. I have tried this.Dispose but it does not work.
On the main page that loads the user controls dynamically on adding I have:
protected void cmdsubmitsofficecat_Click(object sender, EventArgs e)
{
int retvalue = 0;
SQLConnectivity db = new SQLConnectivity();
SqlParameter[] param = new SqlParameter[8];
List<group.category> flist = new List<group.category>();
param[0] = db.MakeInputParameter("@group_id", 1);
param[1] = db.MakeInputParameter("@parent_id", DBNull.Value);
param[2] = db.MakeInputParameter("@category_name", this.txtofficecat.Text.Trim());
param[3] = db.MakeInputParameter("@description", this.txtofficecat.Text.Trim());
param[4] = db.MakeInputParameter("@organization_id", 1);
param[5] = db.MakeInputParameter("@created_by", 1);
param[6] = db.MakeInputParameter("@is_active", 1);
param[7] = db.MakeOutputIntegerParameter("@category_id");
db.RunNonQueryProcedure("PerformanceCategorySave", param, ref retvalue);
if (retvalue != 0)
{
group.category item = new group.category();
item.catid = retvalue;
item.catname = this.txtofficecat.Text.Trim();
item.desc = this.txtofficecat.Text.Trim();
item.isactive = true;
item.parid = 0;
catctl ctl = (catctl)Page.LoadControl("~/catctl.ascx");
ctl.groupid = 1;
ctl.organizationid = 1;
ctl.categoryid = item.catid;
ctl.categoryname = item.catname;
ctl.parentid = item.parid;
ctl.ctltype = 1;
ctl.clist = item.categories;
ctl.plist = item.points;
this.officephld.Controls.Add(ctl);
this.switch_officefields(false, 1);
}
}
On the web user control I have a Page_Load method as:
protected void Page_Load(object sender, EventArgs e)
{
Label ctname = (Label)this.FindControl("ctl0_catname");
this.catname.Text = categoryname;
this.cmdaddcat.Attributes.Add("catid", categoryid.ToString());
this.cmddeletecat.ID = "cmddeletecat" + categoryid.ToString();
this.cmddeletecat.Click += new ImageClickEventHandler(this.cmddeletecat_Click);
if ( clist != null && clist.Count > 0 )
{
int i = 0;
this.cmddeletecat.Visible = false;
foreach ( group.category item in clist )
{
catctl ctl = (catctl)Page.LoadControl("~/catctl.ascx");
ctl.categoryid = item.catid;
ctl.categoryname = item.catname;
ctl.organizationid = organizationid;
ctl.groupid = groupid;
ctl.parentid = item.parid;
ctl.clist = item.categories;
ctl.plist = item.points;
ctl.ctltype = ctltype;
this.newphld.Controls.Add(ctl);
}
}
if ( plist != null && plist.Count > 0 )
{
this.rptPts.Visible = true;
this.rptPts.DataSource = plist;
this.rptPts.DataBind();
this.cmddeletecat.Visible = false;
}
this.Switch_Usability();
}
and a delete method as:
protected void cmddeletecat_Click(object sender, ImageClickEventArgs e)
{
List<group.category> flist = new List<group.category>();
SQLConnectivity db = new SQLConnectivity();
SqlParameter[] param = new SqlParameter[1];
DataTable dt = new DataTable();
DataTable sdt = new DataTable();
group parent = new group();
param[0] = db.MakeInputParameter("@category_id", categoryid);
db.RunNonQueryProcedure("PerformanceCategoryDelete", param);
if ( Page != null )
{
PlaceHolder ctl = (PlaceHolder)Page.FindControl("officephld");
if ( ctl != null )
{ ctl.Controls.Remove(this); }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
newlyCreatedControl.Parent.Controls.Remove(newlyCreatedControl)
方法来删除此newlyCreatedControl。You can use the
newlyCreatedControl.Parent.Controls.Remove(newlyCreatedControl)
method to remove this newlyCreatedControl.如果删除按钮直接位于控件内部,则“this”将引用该控件,并且您可以使用父级来获取控件容器。
如果删除按钮位于占位符或控件内的另一个容器控件内,则必须使用 Parent 两次:
如果将此代码放在删除单击事件中并将鼠标悬停在“this”关键字上,Intellisense 应该向您显示这指的是你的控制类。
例如,我的动态控件有一个占位符,我可以在其中注入工具栏控件。所以要到达实际的模块,我必须使用 Parent.Parent (参见下图,当我将鼠标悬停在其上时,它会显示智能感知)。 “this”指的是控件(在我的例子中是工具栏),第一个父级是插入工具栏的占位符,第二个父级是我动态添加到页面的实际模块。
如果我第三次调用父级,它将进入包含动态控件的容器。
If the delete button is directly inside the control, "this" will refer to the control and you can use the parent to get the controls container.
If the delete button is inside of a placeholder or another container control inside of the control, you will have to use the Parent twice:
If you place this code in the delete click event and hover over the "this" keyword, Intellisense should show you that this refers to your control class.
For example, my dynamic controls have a placeholder where I inject a toolbar control. so to get to the actual module I have to use Parent.Parent ( see the image below, it shows the Intellisense when I hover over this). "this" refers to the control (in my case the toolbar), the first parent is the Placeholder where the toolbar is inserted and the second parent is the actual module that I am adding to the page Dynamically.
If I were to call parent a third time it would step up to the container that contains the dynamic control.