1

我应该创建一个事件触发LabelTextBox时间。CheckBoxListSelectedIndexChanged

我尝试使用以下代码,但它给了我这个错误:

找到具有相同 ID 'mylab1' 的多个控件

protected void chkCar_SelectedIndexChanged(object sender, EventArgs e)
{
    // Panel2.Controls.Clear();
    string dat = DateTime.Parse(GridView1.SelectedRow.Cells[4].Text).ToString("dd-MMM-yyyy");
    lblName.Text = "";
    for (int i = 0; i < chkCar.Items.Count; i++)
    {
        int sum = 0;
        if (chkCar.Items[i].Selected == true)
        {
            Panel tt = new Panel();
            Panel2.Controls.Add(new LiteralControl("<br />"));
            Label mylab = new Label();
            mylab.ID = "mylab" + NextID1;
            mylab.Text = "No of Persons in " + chkCar.Items[i].Value;
            Panel2.Controls.Add(mylab);
            ControlsList1.Add(mylab.ID);

            TextBox mytext = new TextBox();
            mytext.ID = "mytext" + NextID2;
            mytext.Text = "";
            Panel2.Controls.Add(mytext);
            ControlsList2.Add(mytext.ID);
        }
    }
}

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);
    foreach (string lbID in ControlsList1)
    {
        Label lb = new Label();
        lb.ID = lbID;
        Panel2.Controls.Add(lb);
        Panel2.Controls.Add(new LiteralControl("<br>"));
    }
    foreach (string txtID in ControlsList2)
    {
        TextBox tb = new TextBox();
        tb.ID = txtID;
        Panel2.Controls.Add(tb);
        Panel2.Controls.Add(new LiteralControl("<br>"));
    }
}

private List<string> ControlsList1 //labels
{
    get
    {
        if (ViewState["controls"] == null)
        {
            ViewState["controls"] = new List<string>();
        }
        return (List<string>)ViewState["controls"];
    }                     
}

private List<string> ControlsList2 //textboxes
{
    get
    {
        if (ViewState["controls"] == null)
        {
            ViewState["controls"] = new List<string>();
        }
        return (List<string>)ViewState["controls"];
    }
}

private int NextID1//label
{
    get
    {
        return ControlsList1.Count + 1;
    }
}

private int NextID2//textbox
{
    get
    {
        return ControlsList2.Count + 1;
    }
}

谁能帮助我知道我哪里出错了?提前致谢。

4

3 回答 3

1

我想要完成这项工作,您需要将新添加的控件 ID 添加到ControlsList1and ControlsList2,否则NextID1并且在循环NextID2期间将始终返回相同的数字。for

if (chkCar.Items[i].Selected == true)
{
    Panel tt = new Panel();
    Panel2.Controls.Add(new LiteralControl("<br />"));
    Label mylab = new Label();
    mylab.ID = "mylab" + NextID1;
    mylab.Text = "No of Persons in " + chkCar.Items[i].Value;
    Panel2.Controls.Add(mylab);
    ControlsList1.Add(mylab.ID) // <--- here

    TextBox mytext = new TextBox();
    mytext.ID = "mytext" + NextID2;
    mytext.Text = "";
    Panel2.Controls.Add(mytext);
    ControlsList2.Add(mytext.ID) // <--- and here
}
于 2013-07-25T12:02:14.710 回答
1

May be just try not to load ViewState and clear your controls every time you fire your checkbox event. By the way why are you loading ViewState? And if ViewState already loads your controls and then your checkbox again tries to load controls in your panel.

Its a blind shot hope it works.

于 2013-07-25T13:00:49.727 回答
0

问题在于您NextID1生成递增 id 的方法,它没有给您唯一的数字。您可以使用循环变量使 id 唯一。我看不到任何添加控件的代码,ControlsList1以便方法 NextID1 每次添加控件时都会给出下一个数字ControlsList1

private int NextID1//label
{
    get
    {
        return ControlsList1.Count + 1;
    }
}

你也没有把(分配)ControlsList1ViewState

于 2013-07-25T12:00:20.437 回答