2011-09-11 86 views
1

我有一個動態加載用戶控件的網頁。每個用戶控件都可以遞歸地在它自己內部加載相同的控件。每個控件都有一個刪除按鈕。我遇到的問題是,當用戶點擊刪除按鈕,我需要刪除的刪除按鈕包含在控制刪除動態用戶控件

頁面控件集合可以如下所示:

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); } 
    } 
} 
+0

是不是這個ctl.Controls.Remove(this);加工?在你之間也試一試cmddeletecat_Click - > this.NamingContainer.Controls.Remove(this); – Waqas

回答

2

您可以使用newlyCreatedControl.Parent.Controls.Remove(newlyCreatedControl)方法刪除這個新的lyCreatedControl。

+0

我在哪裏可以找到這個newlyCreatedControl屬性? – mattgcon

+0

newCreatedControl是新添加的類別的ID ..它可以是任何東西 – Waqas

+0

哦,所以在C#ASP.NET中newCreatedControl將是「this」 – mattgcon

0

如果刪除按鈕直接位於控件內部,則「this」將引用該控件,並且您可以使用父項來獲取控件容器。

this.Parent.Controls.Remove(this) 

如果刪除按鈕是一個佔位符或控制的內部另一個容器控件中,你將不得不使用父兩次:

this.Parent.Parent.Controls.Remove(this) 

如果您在刪除單擊將此代碼事件並將鼠標懸停在「this」關鍵字上,Intellisense會告訴您這是指您的控件類。

例如,我的動態控件有一個佔位符,我注入了一個工具欄控件。所以要得到實際的模塊,我必須使用Parent.Parent(請參閱下圖,它顯示了我將鼠標懸停在此上方時的Intellisense)。 「this」是指控件(在我的情況下是工具欄),第一個父級是插入工具欄的佔位符,第二個父級是我動態添加到頁面中的實際模塊。 Intellisense

如果我第三次調用父進程,它將進入包含動態控制的容器。