<%@ Page Language="VB" Trace="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server"> Public Class ControlColl Inherits CollectionBase Public Function Add(ByVal [Control] As Control) As Control Me.List.Add([Control]) Return [Control] End Function Default ReadOnly Property Item(ByVal index As Int32) As Control Get Return CType(Me.List.Item(index), Control) End Get End Property End Class Public Property CollectionOfControl() As ControlColl Get Return CType(Session("CollectionOfControl"), ControlColl) End Get Set(ByVal value As ControlColl) Session("CollectionOfControl") = value End Set End Property Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) If Not Page.IsPostBack Then CollectionOfControl = New ControlColl Else For Each c As Control In CollectionOfControl ph1.Controls.Add(c) Next End If End Sub Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim txt As New TextBox txt.Text = "Cyril" txt.ID = "Txt1" ph1.Controls.Add(txt) CollectionOfControl.Add(txt) End Sub Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) button2.Text = "button cliqué" End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <form id="form1" runat="server"> </form> </html>
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionpublic int myVar; public int MyProperty { get { return myVar;} set { myVar = value;} } Public myVar As Integer Public Property MyProperty() As Integer Get Return myVar End Get Set(ByVal value As Integer) myVar = value End Set End Property
Default ReadOnly Property Item(ByVal index As Int32) As Control Get Return CType(Me.List.Item(index), Control) End Get End Property
Control Item { get { return ((Control)this.List[index]); } }
public int index { get { return index; } set{ index=value; } }
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace datagrid { public class WebForm3 : System.Web.UI.Page { public System.Web.UI.WebControls.Button Button1; public System.Web.UI.WebControls.Button Button2; protected System.Web.UI.WebControls.PlaceHolder ph1; #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN : Cet appel est requis par le Concepteur Web Form ASP.NET. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas /// le contenu de cette méthode avec l'éditeur de code. /// </summary> private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Button2.Click += new System.EventHandler(this.Button2_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Page_Init(object sender, System.EventArgs e) { InitializeComponent(); } public class ControlColl : CollectionBase { public Control Add(Control lControl) { this.List.Add(lControl); return lControl; } } public ControlColl CollectionOfControl { get { return ((ControlColl)Session["CollectionOfControl"]); } set { Session["CollectionOfControl"] = value; } } public void Button1_Click(object sender, System.EventArgs e) { TextBox txt = new TextBox(); txt.Text = "Cyril"; txt.ID = "txt1"; ph1.Controls.Add(txt); CollectionOfControl.Add(txt); } public void Button2_Click(object sender, System.EventArgs e) { Button2.Text = "button cliqué"; } private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { CollectionOfControl = new ControlColl(); } else { foreach (Control c in CollectionOfControl) { ph1.Controls.Add(c); } } } } }
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server"> public class ControlColl : CollectionBase { public Control Add(Control lControl) { this.List.Add(lControl); return lControl; } } public ControlColl CollectionOfControl { get { return ((ControlColl)Session["CollectionOfControl"]); } set { Session["CollectionOfControl"] = value; } } public Int32 IDCount { get { return ((Int32)Session["IDCount"]); } set { Session["IDCount"] = value; } } public void Button1_Click(object sender, System.EventArgs e) { TextBox txt = new TextBox(); txt.Text = "Cyril"; txt.ID = "txt" + @IDCount; ph1.Controls.Add(txt); CollectionOfControl.Add(txt); IDCount ++; } public void Button2_Click(object sender, System.EventArgs e) { Button2.Text = "button cliqué"; } private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { CollectionOfControl = new ControlColl(); IDCount = 0; } else { foreach (Control c in CollectionOfControl) { ph1.Controls.Add(c); } } } // Public Property CollectionOfControl() As ControlColl // Get // Return CType(Session("CollectionOfControl"), ControlColl) // End Get // Set(ByVal value As ControlColl) // Session("CollectionOfControl") = value // End Set // End Property // // // Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) // // If Not Page.IsPostBack Then // CollectionOfControl = New ControlColl // Else // For Each c As Control In CollectionOfControl // ph1.Controls.Add(c) // Next // End If // // End Sub // // // Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) // // Dim txt As New TextBox // txt.Text = "Cyril" // txt.ID = "Txt1" // ph1.Controls.Add(txt) // CollectionOfControl.Add(txt) // // End Sub // // Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) // button2.Text = "button cliqué" // End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <form id="form1" runat="server"> </form> </html>