The Solution is pretty simple really each one of the controls you are creating dynamically has the have an id irrespective if you assign one or not without delving too deep into the inner workings of asp.NET, lets just say that if you don't assign a value to the ID property of the control aspnet will, the names are all in the format _ctl1,_ctl2 etc... Whether it is expected behavior or some bug in .Net I'm not sure but from time to time multiple controls on a page will end up with the same ID (Check the Page Source of your page and you will see this) this is especially problematic when you add event handlers to your controls. So basically I just used a long winded way of saying assign a unique value to each of the controls you are adding to the page.
Ie. myC.ID = "SomeUniqueValue"; -----Original Message----- From: Oren Novotny [mailto:osn@;PO.CWRU.EDU] Sent: 14 November 2002 02:57 PM To: [EMAIL PROTECTED] Subject: Re: [ADVANCED-DOTNET] Exception when using Trace with cached controls Anyone manage to come up with any solutions or alternate ideas to my problem, or have I managed to stump the great minds on this list ;) I've already tried looking elsewhere, on Google, MSDN, and the ng's, to no avail; this list was my last defense... Is there really no way to get tracing working with what I'm trying to do? Thanks, --Oren > -----Original Message----- > From: Moderated discussion of advanced .NET topics. [mailto:ADVANCED- > [EMAIL PROTECTED]] On Behalf Of Oren Novotny > Sent: Monday, November 11, 2002 7:53 AM > To: [EMAIL PROTECTED] > Subject: Exception when using Trace with cached controls > > 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. You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.
