<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DynamicButton" %>
Untitled Page
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DynamicButton : System.Web.UI.Page
{
private void Page_Load(object sender, EventArgs e)
{
Button newButton = new Button();
newButton.Text = "Dynamic Button";
newButton.ID = "newButton";
newButton.Click += new System.EventHandler(this.Button_Click);
PlaceHolder1.Controls.Add(newButton);
}
protected void Button_Click(object sender, System.EventArgs e)
{
Label1.Text = "You clicked the dynamic button.";
}
protected void cmdReset_Click(object sender, System.EventArgs e)
{
Label1.Text = "(No value.)";
}
protected void cmdRemove_Click(object sender, System.EventArgs e)
{
Button foundButton = (Button)Page.FindControl("newButton");
if (foundButton != null)
{
foundButton.Parent.Controls.Remove(foundButton);
}
}
}