1

我正在添加动态控件,并且我想在动态添加控件时增加 Label 中的值

代码:

private int controlCount
    {
        get
        {
            int val = 0;
            try
            {
                val = (int)ViewState["ControlCount"];
            }
            catch (Exception e)
            {
                // handle exception, if required.
            }
            return val;
        }
        set { ViewState["ControlCount"] = value; }
    }

    protected void addnewtext_Click(object sender, EventArgs e)
    {
        int i = controlCount++;
        for (int j = 0; j <= i; j++)
        {
            AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
            Label lb = new Label();
            string z = Convert.ToString(i + 1);
            lb.Text = "Visa " + z;
            rpt1.Controls.Add(lb);
            lb.Attributes.Add("class", "style8");
            rpt1.Controls.Add(ac);
            rpt1.Controls.Add(new LiteralControl("<BR>"));
        } 
    }

在下图中,我得到标签值,即(Visa 3)正在覆盖

在此处输入图像描述

有任何想法吗?

4

1 回答 1

2

你要

string z = Convert.ToString(j + 1); 

而不是

string z = Convert.ToString(i + 1);
于 2013-09-24T13:56:39.017 回答