I have created some file upload control at run time in a panel..now i want to remove control on a click of link button.
how can i do this..
following is the code for crating control dynamically..
protected void LinkButton1_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
newattach(count);
count++;
}
private void newattach(int tot)
{
int i;
for (i = 0; i < tot; i++)
{
f1 = new FileUpload();
f1.ID = "FileUpload" + count.ToString();
f1.Height =20;
f1.Width = 150;
Panel1.Controls.Add(f1);
}
}
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:
Other Important Notes:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With