Shawn, I wouldn't go as far as calling it a bug. It's simply how the framework was implemented. As expected, the page and the parent for the validator is set when it is added to the Controls collection of its parent. Then, during the validators OnInit() method the validator adds itself to the Page.Validators collection. When you remove the validator from the Controls collection (or remove a control that contains a validator), the framework recursively unloads the controls and eventually calls the OnUnload event of each control in the heiarchy. The OnUnload() of the validator removes itself from the Page.Validators collection.
Now when you add the validators back (or controls that contain validators), the validator's OnInit() method (which adds itself to the Page.Validators) is not called since the control has already been initialized. One of the causes of your problem stems from the fact that the Control class recursively initializes it's children (and calling the respective OnInit() methods) before calling it's own OnInit(). As an alternative, you could try override the DeterminePostBackMode() method on the page (calling base also) and plug in just prior to the InitRecursive call and do your shuffling there. That way the validators have not yet added themselves to the Page.Validators collection. I'm not certain about this but you may need to wait to add your container control which now contains all the form's controls until the page's OnInit(). Chad On Thu, 5 Feb 2004 15:50:51 -0500, Shawn Wildermuth <[EMAIL PROTECTED]> wrote: >The problem, very specifically, is that if you remove controls from a page >and add them back in (dynamically in code), any validators get disconnected >from the page. I fixed it by adding them back manaually, but I think that >it is a bug in ASP.NET and am about to an incident with MS. > > >Thanks, > >Shawn Wildermuth >[EMAIL PROTECTED] >Author of Pragmatic ADO.NET >http://adoguy.com/book >http://ONDotnet.com >Microsoft .NET MVP > >> -----Original Message----- >> From: Moderated discussion of advanced .NET topics. >> [mailto:[EMAIL PROTECTED] On Behalf Of J. Merrill >> Sent: Thursday, February 05, 2004 10:44 AM >> To: [EMAIL PROTECTED] >> Subject: Re: [ADVANCED-DOTNET] Fiddling with Controls >> collection in ASP.NET >> >> You said that the problem doesn't happen if you "embed a form >> in a UserControl." Could you create a new page, load your >> master UserControl onto it, and embed the form in that UserControl? >> >> Since I don't know exactly what you mean by "embed a form in >> a UserControl" I can't be sure that this solves your problem. >> >> (When you gather all the controls and add them back, does >> that carry any child controls along?) >> >> Good luck... >> >> At 04:20 PM 2/4/2004, Shawn Wildermuth wrote >> >Caveat: I decided to post it to Advanced since what I am >> trying to do >> >is a little ASP.NET black magic. >> > >> >I have created a base ASP.NET class that derives from >> System.Web.UI.Page. >> >This class's job is to create a master page for all Pages in a >> >particular application. In this class I do the following: >> > >> >- On it's OnInit() call, I gather all the controls for the >> > page's HtmlForm object and remove them from the HtmlForm's >> > Controls collection. >> >- I then load a specific UserControl that contains the master >> > page information. >> >- Lastly, I add all the controls that used to live in the >> > HtmlForm'ss Control's collection to a control within >> > the Master Page User Control. >> > >> >All of this worked well until I ran into validators. Quick >> background >> >on client-side validation. When a page has validators, >> client scripts >> >are added to the page and ultimately called from a submit >> button on the page. >> >After generating a page, a typical button goes from: >> > >> ><asp:Button id="Button1" runat="server" Text="Button"></asp:Button> >> > >> >And makes it: >> > >> ><input type="submit" >> > name="WebUserControl11:Button1" >> > value="Button" >> > onclick="if (typeof(Page_ClientValidate) == 'function') >> >Page_ClientValidate(); " >> > language="javascript" >> > id="WebUserControl11_Button1" /> >> > >> >The magic is the onclick event. This forces the page to >> validate. The >> >problem is that with my base page class this fixup when it >> renders the >> >button never happens. I've delved into a myriad of possible >> solutions >> >and have concluded the following information: >> > >> >- Changing the time when the control fiddling happens >> > seems to have no affect. >> >- Embedding a form within another control (using a User >> > Control) does not make this happen. >> >- Commenting out the code that does the re-parenting of >> > the controls in my base class resolves the issue (but >> > obviously doesn't really fix it). Therefore the >> > subclassing of the Page class is not the issue. >> > >> >What I am looking for is some insight to how the validator >> code fix-ups >> >happen. I am happy to reverse engineer the rendering to see when it >> >happens to find clues as how to get around the issue, but I >> haven't had >> >any success with Reflector yet. Anyone run into this or can >> give me an >> >idea of where to start to look about where the fixup >> rendering happens? >> > >> >TIA >> > >> > >> >Thanks, >> > >> >Shawn Wildermuth >> >[EMAIL PROTECTED] >> >Author of Pragmatic ADO.NET >> >http://adoguy.com/book >> >http://ONDotnet.com >> >Microsoft .NET MVP >> >> >> J. Merrill / Analytical Software Corp >> >> =================================== >> This list is hosted by DevelopMentor. http://www.develop.com >> Some .NET courses you may be interested in: >> >> NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles >> http://www.develop.com/courses/gaspdotnetls >> >> View archives and manage your subscription(s) at >> http://discuss.develop.com >> > >=================================== >This list is hosted by DevelopMentor� http://www.develop.com >Some .NET courses you may be interested in: > >NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles >http://www.develop.com/courses/gaspdotnetls > >View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor� http://www.develop.com Some .NET courses you may be interested in: NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles http://www.develop.com/courses/gaspdotnetls View archives and manage your subscription(s) at http://discuss.develop.com
