我在面板中运行时创建了一些文件上传控件..现在我想删除点击链接按钮的控件.
我怎样才能做到这一点..
以下是动态控制箱子的代码..
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);
}
}
Run Code Online (Sandbox Code Playgroud)
如上所述,亚光?;)
Panel1.Controls.Remove(Panel1.FindControl("FileUploadID"));
Run Code Online (Sandbox Code Playgroud)
应该工作,但是:
通过调试器运行整个页面,你会发现一些奇怪的东西......并且不了解.Net中的页面生命周期以及关于动态控件的基本原理,你可能会重新出现你的控件,这取决于页面生命周期中的时间你确实创建了动态控件,所以问题的答案更多的是如何以可控的方式创建正确的动态控件.所以:
asp.net中的动态控件 - Yuriy Solodkyy的那些原则适用于:
按照这种一致的方法动态创建控件:
其他重要说明:
我通常在页面模板后面使用以下代码:
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
Run Code Online (Sandbox Code Playgroud)