Asp: Literal null control in user management

I have a user control containing asp: Literal.

<div>
     <asp:Literal id="MenuContainer" runat="server" />
</div>

There is code on the page with the code that initializes the control:

internal void Setup(MyBusinessObject obj)
{
    MenuObject menu = MenuHelper.GetMenu(obj.State);

    if(obj == null)
        MenuContainer.Visible = false;

    //other code
}

On the page where the control is used, I call the Control Settings method in the LoadComplete event handler (I first called it in the Load event). Regardless of whether MyBusinessObject is null or not, when I access Literal under user control, I get an error:

Object reference not set to an instance of an object.

What is the reason and why is this tool?

+5
source share
7 answers

. web.config, (:( , , ).

, @Register, , null! @Register .

+11

, web.config. , ( ):

<add tagPrefix="prefix" namespace="example.ui.controls" assembly="example.ui" />

, , , @Register.

<add tagPrefix="prefix" tagName="Message" src="~/Controls/Message.ascx" />
+5

, TheVillageIdiot, - .

<add tagPrefix="user" namespace="Frontend.Web.UserControlsAccount" assembly="Frontend.Web" />

web.config , ! :

<user:ucLoginMessages runat="server" ID="Msgs" />

... , UserControl .

<%@ Register Src="~/UserControlsAccount/LoginMessages.ascx" TagPrefix="user" TagName="Messages" %>

: -)

!

+1

, MenuContainer? obj.State Setup. obj null, .

0

JerSchneid, obj null, . , :

internal void Setup(MyBusinessObject obj)
{
    if(obj == null)
        MenuContainer.Visible = false;
    else
        MenuObject menu = MenuHelper.GetMenu(obj.State);
}

EDIT: , , . , , MenuContainer.Visible = false;.

0

MenuContainer null, , , - . MenuContainer. Setup Page_Load?

0

:

internal void Setup(MyBusinessObject obj)
{    
    MenuObject menu = MenuHelper.GetMenu(obj.State);    

    if(obj == null)        
        MenuContainer.Visible = false;    //other code
}
  • obj null, obj.State NullReferenceException

  • obj , MenuContainer.Visible = false .

, .

When you are having difficulty debugging these kinds of things, try running the code with the debugger or adding some confirmation to your code, which will help you see exactly what is going on:

internal void Setup(MyBusinessObject obj)
{    
    Debug.Assert(obj != null);
    MenuObject menu = MenuHelper.GetMenu(obj.State);    

    Debug.Assert(MenuContainer != null);
    if(obj == null)        
        MenuContainer.Visible = false;    //other code
}
0
source

All Articles