+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Acessing control in Page_Load of normal user control results in Object not found

Acessing control in Page_Load of normal user control results in Object not found

2 posts, 0 answered
  1. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    15 May 2013
    Link to this post
    I'm trying to implement a menu where I can dynamically add a form to one of the menus as apposed to the regular sub menus. The rest of the menus will have sub menus.

    The following is the Ascx code:
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MajorMenu.ascx.cs" Inherits="Bounce.Web.Widgets.Navigation.MajorMenu" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
     
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />
    <telerik:RadMenu OnItemDataBound="MajorRadMenu_ItemDataBound" ID="MajorRadMenu" runat="server" DataSourceID="SitemapDataSource1" MaxDataBindDepth="1" OnItemClick="MajorRadMenu_ItemClick">
    </telerik:RadMenu>

    and here is the CS:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    namespace Bounce.Web.Widgets.Navigation
    {
        public partial class MajorMenu : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                MajorRadMenu.ItemDataBound += new Telerik.Web.UI.RadMenuEventHandler(MajorRadMenu_ItemDataBound);
            }
     
            void MajorRadMenu_ItemDataBound(object sender, Telerik.Web.UI.RadMenuEventArgs e)
            {
                Response.Write("Hello");
            }
        }
    }

    After adding this to the toolbox and then dropping it on the page and viewing the resulting page I get the following error: (Note: This happens whether I use the Telerik control or just a plain ASP.net textbox)
    Server Error in '/' Application.
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during
    the execution of the current web request. Please review the stack trace
    for more information about the error and where it originated in the
    code.
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:
    Line 12: protected void Page_Load(object sender, EventArgs e)
    Line 13: {
    Line 14: MajorRadMenu.ItemDataBound += new Telerik.Web.UI.RadMenuEventHandler(MajorRadMenu_ItemDataBound);
    Line 15: }
    Line 16:
    Source File: f:\Projects\Xpo\Branches\Dev\Applications\Xpo.Website\Xpo.BounceLogistics.Web\Widgets\Navigation\MajorMenu.ascx.cs    Line: 14
    Stack Trace:
    [NullReferenceException: Object reference not set to an instance of an object.]
    Bounce.Web.Widgets.Navigation.MajorMenu.Page_Load(Object sender, EventArgs e) in f:\Projects\Xpo\Branches\Dev\Applications\Xpo.Website\Xpo.BounceLogistics.Web\Widgets\Navigation\MajorMenu.ascx.cs:14
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
    System.Web.UI.Control.OnLoad(EventArgs e) +92
    System.Web.UI.Control.LoadRecursive() +54
    System.Web.UI.Control.LoadRecursive() +145
    System.Web.UI.Control.LoadRecursive() +145
    System.Web.UI.Control.LoadRecursive() +145
    System.Web.UI.Control.LoadRecursive() +145
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18033

    Not sure what we're doing wrong here?

    Any help would be greatly appreciated.
    EDIT: We've added this user control directly to the master page and it works perfectly. So it only fails when we try to add it to the toolbox of Sitefinity

    Thanks,
    Jacques
  2. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    18 May 2013
    Link to this post
    Hello,

    I have placed the code on a user control locally and I did not receive the Object reference.. error. However it is advisable to use the SitefinitySitemapDataSource instead for data source of the RadMenu. Additionally when binding RadMenu to a SitemapDataSource the server side ItemClicked event will not fire - take a look at the following article for more information. Try out the markup below:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="SitefinityWebApp.TestControl" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.NavigationControls" TagPrefix="sf" %>
     
    <sf:SitefinitySiteMapDataSource ID="SitefinitySiteMapDataSource1" runat="server" ShowStartingNode="false" />
    <%--<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />--%>
    <telerik:RadMenu ID="MajorRadMenu" runat="server" DataSourceID="SitefinitySiteMapDataSource1" MaxDataBindDepth="1" >
    </telerik:RadMenu>

    And code:
    protected void Page_Load(object sender, EventArgs e)
           {
               MajorRadMenu.ItemDataBound += MajorRadMenu_ItemDataBound;
               
           }
     
           void MajorRadMenu_ItemDataBound(object sender, Telerik.Web.UI.RadMenuEventArgs e)
           {
               Response.Write("Hello");
           }

    I hope this helps.

    Kind regards,
    Pavel Benov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered