简体   繁体   中英

Asp.net FindControl + “Multiple controls with the same ID”

I'm getting a really weird behavior with ASP.Net. When I run the following code, the exception "Multiple controls with the same ID" is thrown. The exception is not thrown when adding the control but when using FindControl .

What's really weird is that if I put a breakpoint just before the call and run a FindControl call in the immediate windows where the exception is thrown (so far so consistent) but then when I resume the debugger, everything works fine (!!!). The machine runs the same exact code but, it doesn't throw the exception again.

One last thing about this crazy thing, earlier today the very same code was inside Page_Load and everything was working fine but I reorganized the code and moved it to a separate method (which is called by Page_Load). I'm getting pretty confident this is a ASP.Net bug...

    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

EDIT : I just though of something and it worked :

        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;
        }

So for the time being, my code works fine, but this issue isn't resolved so if someone knows anything about this bug, please enlighten me.

it is not a bug, all your controls has the same ID tb.ID = "Input"; cal.ID = "Input"; , etc

try to add a unique string after each ID like

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

The problem actually came for an event that tried to add those controls again elsewhere in the code .

I just answer my own question so it's a closed matter, no rage downvote for this responsible behavior please.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM