创建动态控件时出错

时间:2013-07-25 11:57:52

标签: c# asp.net

我应该在Label事件TextBox触发时创建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;
    }
}

任何人都可以帮我知道我哪里出错了吗?提前致谢。

3 个答案:

答案 0 :(得分:1)

我认为要完成这项工作,您需要将新添加的控件ID添加到ControlsList1ControlsList2,否则NextID1NextID2将始终返回相同的数字在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
}

答案 1 :(得分:1)

每次触发复选框事件时,可能只是尝试不加载ViewState并清除控件。顺便问一下你为什么要加载ViewState?如果ViewState已加载您的控件,然后您的复选框再次尝试加载面板中的控件。

它是一个盲目的希望它有效。

答案 2 :(得分:0)

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

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

您也没有({分配)ControlsList1ViewState