Search code examples
c#asp.netcustom-controlsascx

ASCX User control not showing up


I'm adding a reports section to an existing web app. The section consists of two pages that get loaded through two different .ascx files. I'm not allowed to modify any of the internal logic as to how the components (mainly the nav bar and the page contents) are loaded. In a normal, regular .aspx page, the reports I set up through Crystal reports show up on the page with no issue. However, when I put the same form into an .ascx page, following the same logic as the content for the other pages, neither this page or a simple "hello world" will load. I've checked if there was some script failing or the container for the content not loading through the developer tools, but no such thing. I've looked around for other possible errors on this website, but no solution has worked so far.

Let me explain how (to my limited understanding) this website loads its contents. There is a master page (Masterpage.Master) that holds a general template for any page it gets loaded in, like the overall site colors, the layout of the main navigation bar, header, content, footer, etc. MasterPage.Master is integrated into an .aspx page along the user controls that will load the navigation bar for the section. In the case of reports, this is where I would put the categories of reports I want to load on the page. This is how I implement this on the reporting section. Every other section of the website works exactly the same way:

<%@ Page Title="" Language="C#" MasterPageFile="~/Project1/Masterpage.Master" AutoEventWireup="true" CodeBehind="Reporting.aspx.cs" Inherits="Project1.Reporting" %>
<%@ Register TagPrefix="sub" TagName="navSubReportsSalesOrders" Src="~/Controls/Navigation/SubSide/navSubReportsSalesOrders.ascx" %>
<%@ Register TagPrefix="sub" TagName="navSubReportsWorkOrders" Src="~/Controls/Navigation/SubSide/navSubReportsWorkOrders.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="SubNav" runat="server">
    <sub:navSubReportsSalesOrders runat="server" Visible="false" ID="navSubReportsSalesOrders" />
    <sub:navSubReportsWorkOrders runat="server" Visible="false" ID="navSubReportsWorkOrders" />
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainBody" runat="server">
    <asp:PlaceHolder ID="contentPlaceHolder" runat="server"></asp:PlaceHolder>
</asp:Content>

And this is the code behind it:

private reportingSalesOrder ReportingSalesOrderControl {get; set;}

        private reportingWorkOrder ReportingWorkOrderControl {get; set;}

        private int screenId { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            navSubReportsSalesOrders.Click += SalesOrdersClick;
            navSubReportsWorkOrders.Click += WorkOrdersClick;

            string Screen = Request.QueryString["ScreenId"];
            if (Screen == "REP001")
                screenId = 1;
            else if (Screen == "REP002")
                screenId = 2;
        }
 //some more code goes here
private void SalesOrdersClick(object sender, EventArgs e)
        {
            Response.Redirect("/Reports.aspx?ScreenId=REP001");
        }

        private void WorkOrdersClick(object sender, EventArgs e)
        {
            Response.Redirect("/Reports.aspx?ScreenId=REP002");
        }

This much works for the reports. The URL is changed on click. However, the problem comes with the second part. The screenId variable is later used to load the content on the page within the contentPlaceHolder tags. The following code works in every section of the application, save for the new reports section I have added.

 protected void OnLoad(object sender, EventArgs e)
        {
            base.OnLoad(e);
           switch (screenId)
            {
                case 1:
                    ReportingSalesOrderControl = (reportingSalesOrder) Page.LoadControl("~/Controls/Reporting/reportingSalesOrder.ascx");
                    contentPlaceHolder.Controls.Add(ReportingSalesOrderControl);
                    break;
                case 2:
                    ReportingWorkOrderControl = (reportingWorkOrder) Page.LoadControl("~/Controls/Reporting/reportingWorkOrder.ascx");
                    contentPlaceHolder.Controls.Add(ReportingWorkOrderControl);
                    break;
                default:
                    ReportingSalesOrderControl = (reportingSalesOrder)Page.LoadControl("~/Controls/Reporting/reportingSalesOrder.ascx");
                    contentPlaceHolder.Controls.Add(ReportingSalesOrderControl);
                    break;
            }

        }

I'm using crystal reports for it, and on an .aspx page, no issues, the reports load. So, I thought it was perhaps that there was some compatibility error that didn't let Crystal Reports load through an ascx file. And so, after backing up my code, I resorted to leave my reportingSalesOrder.ascx file as follows:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="reportingSalesOrder.ascx.cs" Inherits="Project1.Controls.Reporting.reportingSalesOrder" %>
<%@ Register Assembly="DevExpress.Web.v21.1, Version=21.1.12.0, Culture=neutral, PublicKeyToken=somethingsomething" Namespace="DevExpress.Web" TagPrefix="dx" %>

<%@ Register assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=somethingsomething" namespace="CrystalDecisions.Web" tagprefix="CR" %>

<div class="row">
        <h1>HELLO WORLD</h1>
    </div>

Not even this loads. I have a very depressing blank space where the content should be, even if, again, every other element loads with no issue. I have looked at other tutorials, looked at other questions here and while that had let me catch some errors (like using Page.Controls.Add instead of contentPlaceHolder.Controls.Add), it has made no difference on the end result. What could be the problem? I've been stuck on this for days. Any help is welcome.


Solution

  • It ended up being a really silly mistake. I looked again at the other files and the signature of the OnLoad method wasn't protected void OnLoad(object sender, EventArgs e) But rather protected override void OnLoad(EventArgs e)

    Now it works.