Salve Ragazzi,
partendo da uno script che ho trovato online ho impostato una routine che inserisce, ad ogni click su un bottone, un usercontrol all'interno di una TabPanel. Per la rimozione, invece, la funzione è richiamata dallo stesso controllo tramite l'uso di un EventHandler all'interno della pagina che li crea/richiama. La sintassi è questa:
L'UserControl ha il seguente CodeBehind:codice:protected void Button1_Click(object sender, EventArgs e) { counter_01.Text = Convert.ToString(int.Parse(counter_01.Text) + 1); ph1.Controls.Clear(); int ControlID = 0; for (int i = 0; i <= (Convert.ToInt32(counter_01.Text) - 1); i++) { WebUserControl DynamicUserControl = LoadControl("WebUserControl.ascx") as WebUserControl; while (InDeletedList("uc" + ControlID) == true) { ControlID += 1; } DynamicUserControl.ID = "uc" + ControlID; DynamicUserControl.RemoveUserControl += new EventHandler(HandleRemoveUserControl); ph1.Controls.Add(DynamicUserControl); ControlID += 1; } } private bool InDeletedList(string ControlID) { string[] DeletedList = remover_01.Text.Split(new string[] { "|" }, StringSplitOptions.None); for (int i = 0; i < DeletedList.GetLength(0) - 1; i++) { Response.Write(DeletedList.ToString() + " "); if (ControlID.ToLower() == DeletedList[i].ToLower()) { return true; } } return false; } public void HandleRemoveUserControl(object sender, EventArgs e) { Button but = sender as Button; WebUserControl DynamicUserControl = (WebUserControl)but.Parent; string bottone = but.ID; Response.Write(bottone); ph1.Controls.Remove(DynamicUserControl); remover_01.Text += DynamicUserControl.ID + "|"; counter_01.Text = (Convert.ToInt16(counter_01.Text) - 1).ToString(); }
Ma sfortunatamente cliccando btnRemove_Click dell'usercontroll viene eliminato tutto il gruppo dei controlli creati e non il singolo elemento.codice:public event EventHandler RemoveUserControl; protected void btnRemove_Click(object sender, EventArgs e) { if (RemoveUserControl != null) { RemoveUserControl(sender, e); } }
Sapete aiutarmi?

Rispondi quotando