Introduce web user management using Ninject in WebForms

I have an aspx page where I would like to add a link to a user control. The user control is stored in a separate assembly and loaded at run time. After entering the user control, it should be loaded into the page control collection.

Everything seems to be working fine, waiting for the control to be added to the page. There is no error, but the user interface for the control is not displayed.

Global.asax.cs

protected override Ninject.IKernel CreateKernel()
{
    var modules = new INinjectModule[] { new MyDefaultModule() };
    var kernel = new StandardKernel(modules);

    // Loads the module from an assembly in the Bin
    kernel.Load("ExternalAssembly.dll");
    return kernel;
}

and this is how the external module is defined in another assembly:

public class ExternalModule : NinjectModule
{
    public ExternalModule() { }
    public override void Load()
    {
        Bind<IView>().To<controls_MyCustomUserControlView>();
    }
}

The debugger shows that when the application starts, Load is called on the external module, and the dependency is entered on the page.

public partial class admin_MainPage : PageBase
{
    [Inject]
    public IView Views { get; set; }

( ) . , Ninject? , , .

aspx

var c = (UserControl)Views;

// this won't show anything. even the Control collection of the loaded control (c.Controls) is empty
var view = MultiView1.GetActiveView().Controls.Add(c);

// but this works fine
MultiView1.GetActiveView().Controls.Add(new Label() { Text = "Nice view you got here..." });  

, , view/user:

public partial class controls_MyCustomUserControlView : UserControl, IView
{
}

:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyCustomUserControlView.ascx.cs" Inherits="controls_MyCustomUserControlView" %>

<asp:Label Text="Wow, what a view!" runat="server" />
+3
1

, Page.LoadControl .

Page.LoadControl(typeof (controls_controls_MyCustomUserControlView), null) , .LoadControl( "controls_MyCustomUserControlView.ascx" ) .

, VirtualPathProvider VirtualFile, http://www.codeproject.com/KB/aspnet/ASP2UserControlLibrary.aspx

VirtualPathProvider , , , VirtualFile ( ) .

Ninject :

    public override void Load()
    {
        //Bind<IView>().To<controls_MyCustomUserControlView>();
        Bind<IView>().ToMethod(LoadControl).InRequestScope();
    }

    protected IView LoadControl(Ninject.Activation.IContext context)
    {
        var page = HttpContext.Current.Handler as System.Web.UI.Page;
        if (page != null)
        {
            //var control = page.LoadControl(typeof(controls_MyCustomUserControlView), null);
            var control = page.LoadControl("~/Plugins/ExternalAssembly.dll/MyCustomUserControlView.ascx");
            return (IView)control;
        }
        return null;
    }

"" - VirtualPathProvider, .

, , LoadControl. , CodeBehind CodeFile, ASP.NET CodeBehind.

<%@ Control Language="C#" AutoEventWireup="true" 
CodeBehind="~/MyCustomUserControlView.ascx.cs" Inherits="controls_MyCustomUserControlView" %>
+2

Source: https://habr.com/ru/post/1780724/


All Articles