控制缓存复制文本框

时间:2018-06-08 21:26:14

标签: c# asp.net

我正在尝试根据从下拉列表中选择的金额创建文本框;但是说我选择2它显示2个文本框,然后我选择3然后将3个文本框添加到其他2个文本框中,使其成为5个文本框。我试过null控制缓存,但是一个额外的文本框似乎总是在添加。

    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++)
    {
ControlCache = null;
        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 个答案:

没有答案