.Net Dynamicaly Create and Remove a Control / Object, such as a button. Web
private void CreateControls()
{
// Create a New Button
Button myButton = new Button();
// Give the Button Some Properties
myButton.Text = "A Dynamicaly Created Button";
// Add Button to PlaceHolder
// ** NOTE: The Planel must Exist in your design view
Panel1.Controls.Add(myButton);
}
private void RemoveButton()
{
// Search for the button in the Page Controls Collection
Button foundButton = (Button)Page.FindControl("myButton");
// Remove the Button
if (foundButton != null)
{
foundButton.Parent.Controls.Remove(foundButton);
}
}
{
// Create a New Button
Button myButton = new Button();
// Give the Button Some Properties
myButton.Text = "A Dynamicaly Created Button";
// Add Button to PlaceHolder
// ** NOTE: The Planel must Exist in your design view
Panel1.Controls.Add(myButton);
}
private void RemoveButton()
{
// Search for the button in the Page Controls Collection
Button foundButton = (Button)Page.FindControl("myButton");
// Remove the Button
if (foundButton != null)
{
foundButton.Parent.Controls.Remove(foundButton);
}
}
Comments
Post a Comment