I posted this on Thursday today in the -Web list, but so far there's
been no response and I suspect it's too arcane for that list anyway.

I'm getting an exception if I enable output tracing in my app.  The
exception is:

System.Web.HttpException: Multiple controls with the same ID '_ctl1'
were found. Trace requires that controls have unique IDs.

Here is my scenario:  I have a page that contains a bunch of controls of
the same type, just with different parameters.  Each time the control is
loaded, it sets itself up with data from the DB.  What I'm trying to do
is to cache the control so that it doesn't go to the DB every time.  The
control has to display tree of items in it, requiring a recursive DB
call.  I'd like to avoid repopulating often.

I have PlaceHolder p and a bunch of MyControl's that get added to
p.Controls

Here's what I'm doing (pls ignore capitalization, since Word is
auto-formatting):

In Page_Load():

If(Cache["key"] == null)
{
        RefreshCache()
}
else
{
        MyControl[] myControls = (MyControl[])Cache["key"];

        Foreach(MyControl con in myControls)
        {
                p.Controls.Add(con);
        }
}

In RefreshCache():
{
...get data
Foreach(Data d in Datas)
{
        MyControl myC = (MyControl)LoadControl("~/MyControl.ascx");

        // Set a property-- setting this property will trigger
        // it to fill the rest of it's data from the DB
        myC.PropertyId = d.PropID;

        // Add it to the placeholder
        p.Controls.Add(myC);
}

// store it in the cache
MyControl[] myControls = new MyControl[p.Controls.Count];
//Copy the controls to the array
p.Controls.CopyTo(myControls, 0);

Cache.Insert("key", myControls, null, DateTime.Now.AddMinutes(5),
TimeSpan.Zero);
}

I also have a public static method to invalidate the cache when
necessary (if changes are made that would affect the data in the
control).

I can't use OutPutCache, since the control has to deal with some
PostBacks, and you can't do that with an OutPutCache.  Also, I want to
be able to programmatically invalidate it, which you also cannot do with
OutPutCache.

Now, as is, everything does work.  Data is cached after the first load,
and upon a page refresh, the controls are taken from the cache.  If I
then invalidate the cache, it reloads the data.  So far, so good.

But--if I enable page tracing, I get the exception that I mentioned
earlier.  I want to be able to use the tracing, since it provides useful
info, but I also want to cache these controls.

Any thoughts?

--Oren

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to