Results 1 to 10 of 10

Thread: User control in a repeater itemtemplate

  1. #1
    Join Date
    May 2005
    Posts
    13

    Default User control in a repeater itemtemplate

    Hi

    I have a web usercontrol which is placed inside the itemtemplate of a repeater. When the on load event fires for the control the control attempts to access a property which should have been initialised by the application context, but hasn't. The property is initialised correctly when the control is placed on a page outside of a repeater.

    As a work around I've put a check to see if the property is null, if so I access the ApplicationContext directly from the control to retrieve a reference to the object and initiliase the property manually. Obviously this is not the way forward, what am I doing wrong?

    Control config entry:

    Code:
    <object name="HardBopRecords.WebApp.Controls.ImageViewer" type="HardBopRecords.WebApp.Controls.ImageViewer, HardBopRecords.WebApp" singleton="false" lazy-init="false">
    				<property name="ImageProcessingService">
    					<ref object="ImageProcessingService" />
    				</property>	
    			</object>
    Referenced object config entry:

    Code:
    <services xmlns="http&#58;//www.springframework.net" default-lazy-init="true">  
    			<object name="ImageProcessingService" type="HardBopRecords.Services.Image.ImageProcessingService, HardBopRecords.Services">
    </object>
    Reference to control in repeater:

    Code:
    <ItemTemplate>
    						<TR>
    									<TD class="ListTableItem"><HARDBOPRECORDS&#58;IMAGEVIEWER Src='<%#DataBinder.Eval&#40;Container, "DataItem.Image.FileName"&#41;%>' ProcessedSrc="<fileName>_table.jpg" Alt='<%#DataBinder.Eval&#40;Container, "DataItem.Image.Name"&#41;%>' id="Imageviewer1" ActionName="table" ImageFormat="jpg" runat="server"></HARDBOPRECORDS&#58;IMAGEVIEWER></TD>

    Thanks

  2. #2
    Join Date
    Sep 2004
    Location
    Belgrade, Serbia
    Posts
    613

    Default

    It seems like you are not doing anything wrong...

    I haven't had much time to investigate, but it looks like the problem is consequence of the dynamic control creation -- repeater control creates child items based on template *after* all dependency injection code for the page and its contained controls has executed, which means that controls created by the repeater won't have dependencies injected properly :-(

    Solution would be to change DI mechanism for the controls from "push' to "pull", but that opens another can of worms. I'll log this in JIRA and work on it as soon as I have some more time.

    Regards,

    Aleks

  3. #3
    Join Date
    May 2005
    Location
    tyson's corner, va
    Posts
    55

    Default

    is this:
    problem is consequence of the dynamic control creation
    the same reason why the dynamic loading of a control through the LoadControl(path) method does not load the control with injected dependies?

    thanks,

    steve

  4. #4
    Join Date
    Sep 2004
    Location
    Belgrade, Serbia
    Posts
    613

    Default

    Yup, same thing...

  5. #5
    Join Date
    May 2005
    Location
    tyson's corner, va
    Posts
    55

    Default

    I suppose that if there were any workarounds you would have said so. Just in case, however, i'll ask anyway.

    Any workarounds for the LoadControl(path) issue? How about some of that "aleks magic"? [smile]

    Assuming no workarounds or magic, I'm going to have to figure something out. Hopefully it won't be too awful.

    Thanks for your help as usual.

    Steve

  6. #6
    Join Date
    Sep 2004
    Location
    Belgrade, Serbia
    Posts
    613

    Default

    Actually, there is no need for magic in the case of LoadControl method, even with the current implementation.

    There are several options:

    1. Load all the controls in OnInit method, before OnLoad is called. Page.OnLoad calls InjectDependenciesRecursive, passing itself as a parameter, so all the controls that belong to the control hierarchy of the page at this point will have their dependencies injected.

    2. Call InjectDependenciesRecursive directly, passing dynamically loaded control as a parameter -- that's exactly what I used to do in AbstractWizard implementation in order to inject dependencies into dynamically loaded wizard steps.

    3. Get the latest code from CVS, where LoadControl method is overriden in the Page class to inject dependencies into dynamically loaded controls

    Later,

    Aleks

    P.S. Repeater problem still remains though, I still have to look into it.

  7. #7
    Join Date
    May 2005
    Location
    tyson's corner, va
    Posts
    55

    Default

    I just ran a CVS update and didn't pick up any changes to the Page.cs file. My version doesn't have that overriden LoadControl method, so I guess there's a problem somewhere. I noticed SourceForge's web site is under maintenance...perhaps that has something to do with it. I'll try again later...or better yet tomorrow...it's getting late.

    Option 3 is good for me. Thanks for the quick response.

    Best regards,

    Steve

  8. #8
    Join Date
    May 2005
    Location
    tyson's corner, va
    Posts
    55

    Default

    Got it! The controls are loading nicely. Thanks again.

    Best regards,

    Steve

  9. #9
    Mark Pollack is offline Spring.NET Co-Lead Spring TeamSpring User
    Join Date
    Sep 2004
    Location
    New York, NY
    Posts
    1,683

    Default

    Hi Steve,

    Glad it is working. FYI, there is a time lag between developer cvs access and anonymous cvs access. It can be up to 24hrs (AFAIK) so that is "normal" behavior believe it or not.

    Cheers,
    Mark

  10. #10
    Join Date
    Jan 2006
    Location
    Cambridge, UK
    Posts
    1,340

    Default

    As Aleks stated above:

    The solution is to do all LoadControl() and DataBind() calls during OnInit() of your controls & pages. In this case Dependencies will get injected into all your controls - even the ones inside a Repeater.

    If - for some reason - you are not able to call DataBind() during OnInit(), you may inject dependies at any time using a call to

    Code:
      Spring.Util.WebUtils.InjectDependenciesRecursive(IApplicationContext applicationContext, Control control)
    Calling LoadControl() and DataBind() during OnInit() will also resolve an issue with the DataBindingManager and dynamically loaded server-side controls (TextBox etc.): Unbinding Data is done during OnLoad(). If a control gets loaded dynamically during OnLoad(), the HttpRuntime will populate PostData *after* OnLoad() has finished. Thus at the time data is unbound into the controller, not all values will be available at this stage.

    cheers,
    Erich
    Last edited by Erich Eichinger; 08-30-2006 at 07:27 AM.

Similar Threads

  1. Possibility to delete DataBindings
    By sir-archimedes in forum Web
    Replies: 7
    Last Post: 05-06-2006, 07:33 AM
  2. ContentPlaceHolder control behaviour
    By Erich Eichinger in forum Web
    Replies: 5
    Last Post: 03-31-2006, 06:25 PM
  3. Control global dependency injection
    By lahma in forum Web
    Replies: 13
    Last Post: 10-28-2005, 12:30 AM
  4. UserControls
    By boegman in forum Web
    Replies: 7
    Last Post: 06-11-2005, 06:31 PM
  5. Replies: 1
    Last Post: 05-15-2005, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •