Aug 9, 2007

Add update panel dynamically

            private Label Label1;

            protected void Page_Load(object sender, EventArgs e)
            {
            UpdatePanel up1 = new UpdatePanel();
            up1.ID = "UpdatePanel1";

            // Define the button
            Button button1 = new Button();
            button1.ID = "Button1";
            button1.Text = "What time is it?";
            button1.Click += new EventHandler(Button1_Click);

            // Define the literals
            LiteralControl lit = new LiteralControl("
"); // Define the label Label1 = new Label(); Label1.ID = "Label1"; Label1.Text = "[time]"; // Link controls to the UpdatePanel up1.ContentTemplateContainer.Controls.Add(button1); up1.ContentTemplateContainer.Controls.Add(lit); up1.ContentTemplateContainer.Controls.Add(Label1); // Add the UpdatePanel to the list this.Form.Controls.Add(up1); } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToShortTimeString(); }