asp:Literal control null in user control

前端 未结 7 1816
南旧
南旧 2021-01-13 10:51

I\'ve a user control which contains asp:Literal.

相关标签:
7条回答
  • 2021-01-13 10:52

    It was very simple. I was adding things in web.config's controls section as was suggested by Rick Sthral in one of his post ( :( for got about the post, you will have to search on his page).

    It was nicely allowing me to add controls without putting @ Register tag but the downside was that child controls on my controls were shown as null! so I simply put @ Register directive in my pages and it worked.

    0 讨论(0)
  • 2021-01-13 11:00

    Are you sure MenuContainer is the problem? You are referencing obj.State in the first line of the Setup function. If that obj is null you'll get that error.

    0 讨论(0)
  • 2021-01-13 11:05

    Thanks, TheVillageIdiot, for posting the answer to your problem - I ran into exactly the same misunderstanding.

    Adding controls via

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

    in the web.config was not enough to actually use it! I tried it like this on a page:

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

    ... but this would lead to the phenomenon, that none of the controls inside the UserControl were initialized. Only adding

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

    to the top of the page solved the problem :-)

    Thanks again!

    0 讨论(0)
  • 2021-01-13 11:05

    If MenuContainer is null, it probably has something to do with the time line of the page life cycle. You're calling that function before MenuContainer is linked up. Can you try calling Setup in the Page_Load function?

    0 讨论(0)
  • 2021-01-13 11:09

    As mentioned in the answer by JerSchneid, if obj is null, you'll get that error. So, try doing it like this -

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

    EDIT: I know you are getting an error on that line, but just try doing it like this. Or, else, remove the whole code and just keep the MenuContainer.Visible = false; line.

    0 讨论(0)
  • 2021-01-13 11:14

    The code you posted is the following:

    internal void Setup(MyBusinessObject obj)
    {    
        MenuObject menu = MenuHelper.GetMenu(obj.State);    
    
        if(obj == null)        
            MenuContainer.Visible = false;    //other code
    }
    
    • If obj is null, then dereferencing obj.State on the first line will throw a NullReferenceException

    • If obj is not null, the line MenuContainer.Visible = false won't be executed.

    So I don't think you're posting all the relevant code.

    When you're having difficulty debugging this kind of thing, try stepping through the code with the debugger or adding some asserts to your code, which will help you to see exactly what's happening:

    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 讨论(0)
提交回复
热议问题