As answered above by matt ?! ;)
Panel1.Controls.Remove(Panel1.FindControl("FileUploadID"));
Should work, BUT:
Run you whole page trough the debugger you will find some strange things .... And without understanding page life cycle in .Net as well as basic princibles about dynamic controls you might get your control to reappear, depending on when in the page life cycle you do create your dynamic control, so the answer of the question is more about how to create properly dynamic controls in a controllable way. So:
Dynamic controls in asp.net - those principles by Yuriy Solodkyy apply:
Follow this consistent approach to creating controls dynamically:
- Create controls in the CreateChildControls method.
- Call EnsureChildControls in the LoadViewState.
- Wrap and unwrap view state in the Pair object to force calling LoadViewState.
- Save layout of the dynamic part of the page in properties backed in the ViewState.
- Recreate dynamic controls in response to user actions in event handlers.
Other Important Notes:
- Configure your controls before adding them to controls collection of the parent controls.
- Assign unique IDs to dynamically created controls.
- Keep references to dynamically created controls in local fields.
- Remember that post data is processed twice: before OnLoad and after OnLoad.
I generally do use the following code behind page template:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class template : BasePageGui
{
#region Introduction
private string msg; //this is the message you are going to show to your users
private TextBox [ ] holderForTxt; //holder for dynamic textboxes
private DropDownList [ ] holderForDdl;
private HtmlInputRadioButton [ ] holderForRdb;
private HtmlInputCheckBox [ ] holderForCkb;
private Label [ ] holderForAst;
DataSet ds; //dataset for metadata
DataSet pds; //parameter dataset
DataSet rds; //return set dataset
#endregion
#region Properties
//set here page properties to use with the viewstate
#endregion //Properties
#region TemplateMethods
protected override void OnInit ( EventArgs e )
{
} //eof OnInit
protected override void CreateChildControls ()
{
base.CreateChildControls ();
CreateDynamicControls ();
}
protected override object SaveViewState ()
{
return new Pair ( base.SaveViewState () , null );
}
protected override void LoadViewState ( object savedState )
{
base.LoadViewState ( ( (Pair) savedState ).First );
EnsureChildControls ();
}
protected void Page_Load ( object sender , EventArgs e )
{ //comm -- the controls should be generated at the init stage and the databinding happens here
if (this.IsPostBack == false)
{
} //eof on first load
else
{
} //eof on post back
this.DataBind ();
} //eof method
private void CreateDynamicControls ()
{
} //eof method
#endregion //TemplateMethods
#region DisplayMethods
#endregion //DisplayMethods
#region ClickEventHandlers
#endregion ClickEventHandlers
#region GridViewEventHanlders
#endregion //GridViewEventHandlers
} //eof class