ASPX repeater Inconsistent/Unexplained behavior

54 Views Asked by At

I get inconsistent behavior when using repeater. I have a repeater with textboxes inside populated from a DataTable. Everything works perfectly for a while then the following error starts to popup.

Multiple controls with the same ID 'txt' were found. FindControl requires that controls have unique IDs.

The error seems to occur on the second session:

  1. Rebuild & Debug: Everything works perfectly.

  2. Stop debugging & Debug again: Error occurs indefinitely until rebuild.

Any ideas what can cause this ? Thanks in advance.

2

There are 2 best solutions below

1
On

Error shows that you are using same id for textboxes. You must have to use dynamic ids for textboxes like "txt_data_{uniqueId}" . Here uniqueId may be index or can use your primary key of data.

Here you can see answer for image tag : https://forums.asp.net/t/1655369.aspx?Multiple+controls+with+the+same+ID

This will help to you : Set ID of Items In a Repeater

0
On

The solution was pretty straight forward, but I was misled by the fact that this only started to occur on the second debug session.

I was using my own custom TextBox control with children, adding the following to my custom textbox solved the problem.

     public override string ID
    {

        get
        {
            return base.ID;

        }

        set
        {
            base.ID = value;

            if(mTextBox != null)
                mTextBox.ID = "txt" + base.ID;

        }
    }

However I would still like to understand why this problem would only start from the second debug session. I could reload the page 10+ times on the first session without any error and only when I restart debugging the error would appear.

The only cause I can think of is that aspx repeater caches data that conflicts ?