Asp.net FindControl +“具有相同ID的多个控件”

时间:2013-04-24 11:57:03

标签: c# asp.net dynamic-controls

我在ASP.Net上遇到了一个非常奇怪的行为。 当我运行以下代码时,抛出异常“具有相同ID的多个控件”。 添加控件但使用FindControl时不会抛出异常。

真正奇怪的是,如果我在调用之前放置一个断点并在引发异常的即时窗口中运行FindControl调用(到目前为止一直如此),那么当我恢复调试器时,一切工作良好 (!!!)。机器运行相同的确切代码,但它不会再次抛出异常。

关于这个疯狂的事情的最后一件事,今天早些时候,同样的代码在Page_Load中并且一切正常但我重新组织了代码并将其移动到一个单独的方法(由Page_Load调用)。 我非常有信心这是一个ASP.Net错误......

    dlAdvanced.DataSource = dsAdvanced;
    dlAdvanced.DataBind();

    // Load Advanced Values Controls
    #region ADV controls
    foreach (DataListItem dli in dlAdvanced.Items)
    {
        DataRow row = dsAdvanced.Tables[0].Rows[dli.ItemIndex];

        switch ((string)row["Type"])
        {
            default:
                TextBox tb = new TextBox();
                tb.ID = "Input";
                dli.FindControl("InputPlace").Controls.Add(tb);
                break;
            case "System.Int32":
            case "System.Decimal":
                TextBox tbn = new TextBox();
                tbn.ID = "Input";
                Image img = new Image();
                img.SkinID = "NumberRequired";
                img.ApplyStyleSheetSkin(this);
                dli.FindControl("InputPlace").Controls.Add(tbn);
                dli.FindControl("InputPlace").Controls.Add(img); // Exception happens here
                break;
            case "System.DateTime":
                golf.golfControls.CalendarBox cal = new golf.golfControls.CalendarBox();
                cal.ID = "Input";
                cal.SkinID = "Calendar";
                cal.ApplyStyleSheetSkin(this);
                dli.FindControl("InputPlace").Controls.Add(cal);
                break;
            case "System.Boolean":
                RadioButton rb1 = new RadioButton();
                rb1.Text = "True";
                rb1.ID = "Input";
                rb1.GroupName = "grp" + dli.ItemIndex.ToString();
                RadioButton rb2 = new RadioButton();
                rb2.Text = "False";
                rb2.ID = "Input2";
                rb2.GroupName = "grp" + dli.ItemIndex.ToString();
                dli.FindControl("InputPlace").Controls.Add(rb1);
                dli.FindControl("InputPlace").Controls.Add(rb2);
                break;
        }
    }
    #endregion

编辑: 我只想知道一些事情并且有效:

        DataRow row = dsAdvanced.Tables[0].Rows[dli.ItemIndex];

        var inputPlace = dli.FindControl("InputPlace");

        switch ((string)row["Type"])
        {
            default:
                TextBox tb = new TextBox();
                tb.ID = "Input";
                inputPlace.Controls.Add(tb);
                break;
            case "System.Int32":
            case "System.Decimal":
                TextBox tbn = new TextBox();
                tbn.ID = "Input";
                Image img = new Image();
                img.SkinID = "NumberRequired";
                img.ApplyStyleSheetSkin(this);
                inputPlace.Controls.Add(tbn);
                inputPlace.Controls.Add(img);
                break;
            case "System.DateTime":
                golf.golfControls.CalendarBox cal = new golf.golfControls.CalendarBox();
                cal.ID = "Input";
                cal.SkinID = "Calendar";
                cal.ApplyStyleSheetSkin(this);
                inputPlace.Controls.Add(cal);
                break;
            case "System.Boolean":
                RadioButton rb1 = new RadioButton();
                rb1.Text = "True";
                rb1.ID = "Input";
                rb1.GroupName = "grp" + dli.ItemIndex.ToString();
                RadioButton rb2 = new RadioButton();
                rb2.Text = "False";
                rb2.ID = "Input2";
                rb2.GroupName = "grp" + dli.ItemIndex.ToString();
                inputPlace.Controls.Add(rb1);
                inputPlace.Controls.Add(rb2);
                break;
        }

暂时,我的代码工作正常,但是这个问题没有解决,所以如果有人知道这个bug,请赐教。

2 个答案:

答案 0 :(得分:0)

这不是错误,您的所有控件都具有相同的ID tb.ID = "Input"; cal.ID = "Input";

尝试在每个ID之后添加一个唯一的字符串,如

tb.ID = "input" + dli.ItemIndex.ToString();

答案 1 :(得分:0)

问题实际上是针对一个试图在代码中的其他位置再次添加这些控件的事件

我只是回答我自己的问题所以这是一个封闭的问题,请不要为这种负责任的行为做出任何愤怒的投票。