Why I can't add this "custom control" in a .ascx?

Go To StackoverFlow.com

0

My .ascx is into folder /cms/context/ , and it is called Test.ascx (on .NET 4.0, WebForms).

When I try to add :

<%@ Register TagPrefix="iz" Namespace="IZ.WebFileManager" Assembly="IZ.WebFileManager" %>

<div>
    <iz:FileManager ID="FileManager1" runat="server" Height="400" Width="600">
        <RootDirectories>
            <iz:RootDirectory DirectoryPath="~/images/" Text="My Documents" />
        </RootDirectories>
    </iz:FileManager>    
</div>

I get :

System.NullReferenceException. An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Why? And how can I fix it? Thanks

P.S. if I put the same code on Master Page it works as well...

Stack Trace :

[NullReferenceException: Object reference not set to an instance of an object.] IZ.WebFileManager.FileView.OnPreRender(EventArgs e) +167 System.Web.UI.Control.PreRenderRecursiveInternal() +112 System.Web.UI.Control.PreRenderRecursiveInternal() +221 System.Web.UI.Control.PreRenderRecursiveInternal() +221 System.Web.UI.Control.PreRenderRecursiveInternal() +221 System.Web.UI.Control.PreRenderRecursiveInternal() +221 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4200

Here is the link where you can get that plugins. I just put the DLL into Bin folder and write that code into my Web User Control. Always used on Master Page, but in a WUC I have that trouble...

2012-04-05 18:50
by markzzz
you have a stack trace - Mark Schultheiss 2012-04-05 18:54
You can find it on my question : - markzzz 2012-04-05 18:55
Thanks for adding that. "object reference not set to an instance of an object" is the translation for those who need i - Mark Schultheiss 2012-04-05 19:03
sorry, didn't notice the italian language! Any idea - markzzz 2012-04-05 19:15
also added the soruce and what I've done - markzzz 2012-04-05 19:16


2

If you only specify namespace and assembly you will only get the ascx.cs part of the control and not the code-front file (.ascx) Since your code-behind file (.ascx.cs) is dependent on your code-front file (.ascx) to actually instantiate all the stuff you have declared, you need to include the Src in the register directive

<%@ Register Src="~/cms/context/Test.ascx" TagPrefix="iz" TagName="Test" %>

If you create your controls without a code-front file (what used to be called a "custom control") you can get by with only specifying namespace and assembly, but if you want to have the convenience of a code-front file ("user control") you need to specify the source.

All the built-in controls within the asp.net framework are created as Custom Controls which means that it was harder for Microsoft to write them (they couldn't use ascx files) but it's easier for us to consume them (single register line in web.config enables for all pages)

2012-04-05 20:56
by AndreasKnudsen
@markzzz, isn't this answer exactly what I posted below - Jakub 2012-04-11 12:58
@Jakub that is true, but OP apparently misread your answer since he then went on: "...it works as well... I have other code that run..." I just thought I'd provide more context, I didn't mean to rain on your parad - AndreasKnudsen 2012-04-12 05:15


1

I'm pretty sure you are missing the source reference:

<%@ Register Src="~/cms/context/Test.ascx" TagPrefix="iz" TagName="Test" %>
2012-04-05 18:53
by Jakub
you mean when I import the ascx in the .aspx? No, it works as well... I have other code that run.. - markzzz 2012-04-05 18:58
Ads