views:

32

answers:

2

Hi,

I am trying to use this solution to access items inside a telerik menu item:

ascx code:

    <asp:Label ID="DivLeave" runat="server"></asp:Label>

In the ascx.cs file I run this code to disable the asp label

    RadMenuItem expenses = RadMenu1.FindItemByText("Expenses");
    Label DivLeave = (Label)expenses.FindControl("DivLeave");
    DivLeave.Visible = false;

but I get this error when I try to run the code:

    {"Object reference not set to an instance of an object."}

Can anyone tell me how to fix this problem. I really need to run this server side as code surrounding the above code does some work server side and it will all fit in neatly...

Kind regards

A: 

This is because the name of your label is not "DivLeave" when the HTML for your form is rendered. Since it is inside a user control it will be a combination of the user control name on the page and then "DivLeave". You should be able to see the name by looking at the code behind. Also why can't you just reference DivLeave.Visible without using the FindControl? Its an ASP.NET control with the runat server attribute so it should be available to you.

bechbd
The problem with referencing it like DivLeave.Visible is that it is inside a telerik control. For some reason it is not allowing me to do that when it is inside the control...
Rup
A: 

Can you do a quickwatch for 'expenses' object in Visual Studio and see if 'DivLeave' is available? It may so happen that:

  1. The label control is available but at a different level in the object.
  2. The label control itself is not getting added to the parent 'expenses'.

Also, it would be a good idea to do a null check for expenses and DivLeave objects before accessing them.