控制缓存在自动回发时添加文本框

时间:2018-06-08 16:01:38

标签: c#

当用户选择需要的文本框数量时,将填充该数量的文本框。但是,假设用户选择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);

    }


}

0 个答案:

没有答案