NinjectHttpModule Causing Loss of Data in DataBound Controls

36 views
Skip to first unread message

Nathan

unread,
Sep 2, 2010, 7:24:46 PM9/2/10
to ninject
This problem has been baffling me for some time now and I just tracked
the cause down to Ninject, but I cannot imagine why it's happening...

If I set a DataSource for a databound control like a ListView or a
Repeater in a Page_Load event then initiate a PostBack, the Repeater/
ListView is returned with no items in it, but if I bind a label's Text
property to some value, call DataBind() and then initiate a PostBack
the text value is not lost.

This problem seems to directly to pertain to whether or not I have
added the NinjectHttpModule to my web.config because if I remove the
<add /> declaration Repeaters and ListViews again function exactly how
they are supposed to.

Has anyone seen this happen? I have confirmed the problem in two
separate ASP.NET projects.

This is an ASP.NET 4 project demonstrating the problem:
http://www.lakario.com/files/NinjectHttpModuleBug.zip

Should I just use PageBase instead of an HttpModule?
Message has been deleted
Message has been deleted

Charles Vallance

unread,
Sep 2, 2010, 7:45:07 PM9/2/10
to nin...@googlegroups.com
I don't know why Ninject.Web doesn't have a UserControlBase... but here's one I created and use:

namespace Ninject.Web
{
    /// <summary>
    /// A <see cref="UserControl"/> that supports injections.
    /// </summary>
    public abstract class UserControlBase : UserControl
    {
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event to initialize the user control.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
        protected override void OnInit( EventArgs e )
        {
            base.OnInit( e );
            RequestActivation();
        }

        /// <summary>
        /// Asks the kernel to inject this instance.
        /// </summary>
        protected virtual void RequestActivation()
        {
            KernelContainer.Inject( this );
        }
    }
}

On Thu, Sep 2, 2010 at 4:41 PM, Nathan <lak...@gmail.com> wrote:
Clarification: removing the HttpModule from the Web.config caused
UserControls to no longer be injected, but pages that inherit from
PageBase are being injected correctly.

On Sep 2, 5:32 pm, Nathan <laka...@gmail.com> wrote:
> Well I tried disabling the HttpModule and updating all of my pages to
> inherit from PageBase, but it does not appear as though any of my
> values are being activated with this change.
--
You received this message because you are subscribed to the Google Groups "ninject" group.
To post to this group, send email to nin...@googlegroups.com.
To unsubscribe from this group, send email to ninject+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ninject?hl=en.


Nathan

unread,
Sep 2, 2010, 7:56:05 PM9/2/10
to ninject
This seems to have solved the problem for me.

What is it about the code in the NinjectHttpModule that could be
causing the experience I was having? I copied the code from
NinjectHttpModule into PageBase's OnInitComplete event handler and
again received the strange behavior from Repeaters/ListViews as I did
when using the module. This is the code that appears to cause
everything to fail:

/// <summary>
/// Search for usercontrols within the parent control
/// and inject their dependencies using KernelContainer.
/// </summary>
/// <param name="parent">The parent control.</param>
private static void InjectUserControls( Control parent )
{
if ( parent == null || parent.Controls == null )
{
return;
}

foreach ( Control control in parent.Controls )
{
if ( control is UserControl )
{
KernelContainer.Inject( control );
}

InjectUserControls( control );
}
}

On Sep 2, 5:45 pm, Charles Vallance <vallance.char...@gmail.com>
wrote:
> > ninject+u...@googlegroups.com<ninject%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages