Search code examples
asp.netformviewfindcontrolsimple-injector

ASP.NET WebForms and SimpleInjector FindControl throws no parameterless constructor


I have Web Application in Asp.NET (4.5) and Simple Injector. All application works right and Simple Injector too. But the problem is with FormView:

<asp:FormView ID="MyFv" runat="server">
                            <InsertItemTemplate>
                                <label id="myLblTest" runat="server">fffff</label>
                            </InsertItemTemplate>
                        </asp:FormView>

And in cs I try to get this label:

protected void Page_Load(object sender, EventArgs e)
        {
            Control testDDL = MyFv.FindControl("myLblTest");

but testDDL is null and with more complicated formviews with ObjectDataSource it throws an exception:

For this object is not defined constructor with no parameters

StackTrace:

 w System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
       w System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       w System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       w System.Activator.CreateInstance(Type type, Boolean nonPublic)
       w System.Activator.CreateInstance(Type type)
       w System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance)
       w System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)

When this same view was in Website and without SimpleInjector it worked without any problem.

Thanks


Solution

  • The problem was with ObjectDataSource. In event ObjectCreating I assigned to ObjectDataSource class that have parameterless constructor and now it works.