C# 控件缓存在自动回发上添加文本框

C# 控件缓存在自动回发上添加文本框,c#,C#,当用户选择所需的文本框数量时,将填充该数量的文本框。然而,假设用户选择1,然后返回将数量更改为2;然后显示的文本框数量为3。当页面重新加载时,如何阻止它添加 protected List<Control> ControlCache { get { return (List<Control>)(Session["cachedControlsForPageX"] = (Session["cachedControlsForPageX"] as List<Contr

当用户选择所需的文本框数量时,将填充该数量的文本框。然而,假设用户选择1,然后返回将数量更改为2;然后显示的文本框数量为3。当页面重新加载时,如何阻止它添加

protected List<Control> ControlCache
{
    get { return (List<Control>)(Session["cachedControlsForPageX"] = (Session["cachedControlsForPageX"] as List<Control>) ?? new List<Control>()); }
    set { Session["cachedControlsForPageX"] = value; }
}


protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        foreach (var control in ControlCache)
        {
            ContentPlaceHolder1.Controls.Add(control);
            ContentPlaceHolder1.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
        }
    }
    else
        ControlCache = null;
}
protected void ddlCount_SelectedIndexChanged(object sender, EventArgs e)
{



    int count = Convert.ToInt32(ddlCount.SelectedItem.Value);
    for (int i = 0; i < count; i++)
    {

        TextBox tx = new TextBox();
        tx.MaxLength = 10;

        ContentPlaceHolder1.Controls.Add(tx);
        ContentPlaceHolder1.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
        ControlCache.Add(tx);

    }


}
受保护的列表控制缓存
{
获取{return(List)(会话[“cachedControlsForPageX”]=(会话[“cachedControlsForPageX”]作为列表)??新建列表();}
设置{Session[“cachedControlsForPageX”]=value;}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(第IsPostBack页)
{
foreach(ControlCache中的var控件)
{
contentplaceholder 1.Controls.Add(控件);
ContentPlaceholder 1.Controls.Add(新的LiteralControl(“”);
}
}
其他的
ControlCache=null;
}
受保护的无效ddlCount\u SelectedIndexChanged(对象发送方,事件参数e)
{
int count=Convert.ToInt32(ddlCount.SelectedItem.Value);
for(int i=0;i
在添加新文本框之前,请重置ddlCount\u SelectedIndexChanged中的所有内容。@HiteshMistry;给我举个例子,请设置ControlCache=null;ddlCount中的for循环之前_SelectedIndexChanged@Hitesh; 它仍在增加it@Hitesh你能帮我吗