简体   繁体   中英

Crystal Reports Web Viewer Issue with Pagination and Optional Parameters

I've been searching for a couple of days now and am running into an issue no matter what I've tried. The problem is that I seem to have come across with the perfect storm and I can't get all 3 things working at the same time.

  1. Pagination
  2. Optional Parameters
  3. Parameter Dialog Prompt

So this first method is what I've been using and everything works except it won't Navigate past past 2 (And I've very aware of why navigation doesn't work)

// ##################################################################################################################################################
// METHOD 1: Everything works correctly except you can't go past page 2
    protected void Page_Load(object sender, EventArgs e)
    {
        CrystalReportViewer1.ReportSource = Session["myReportDoc"] as CrystalDecisions.CrystalReports.Engine.ReportDocument;

        if (CrystalReportViewer1.ReportSource == null)
        {
            //Generate the Report Document 
            Handlers.ReportHandler myReportHandler = new Handlers.ReportHandler();
            CrystalDecisions.CrystalReports.Engine.ReportDocument myReportDocument = myReportHandler.GenerateReport("AlarmStatusReport");
            Session["myReportDoc"] = myReportDocument;   //This is were we save it off for next time
            CrystalReportViewer1.ReportSource = myReportDocument;
        }
    }

So knowing that the common fix is to not use Page Load but use Page_Init instead. This fixes the Navigation... until I open a report that has optional parameters. With those, every time I try to navigate to the next page, instead of it working, the Parameter box re-appears and now requires at least 1 of my Optional Parameters to be filled out. (Each "next Page" reduces the prompt by 1 Optional). But, because I'm being forced to change the Parameters, it "refreshes" the report and I'm back on Page 1.

    // ##################################################################################################################################################
    // METHOD 2: Works, but not for any report that has Optional Parameters. They become "Required" and keep popping up instead of navigating to the next page
    protected void Page_Init(object sender, EventArgs e)
    {
        CrystalReportViewer1.ReportSource = Session["myReportDoc"] as CrystalDecisions.CrystalReports.Engine.ReportDocument;

        if (CrystalReportViewer1.ReportSource == null)
        {
            //Generate the Report Document 
            Handlers.ReportHandler myReportHandler = new Handlers.ReportHandler();
            CrystalDecisions.CrystalReports.Engine.ReportDocument myReportDocument = myReportHandler.GenerateReport("AlarmStatusReport");
            Session["myReportDoc"] = myReportDocument;   //This is were we save it off for next time
            CrystalReportViewer1.ReportSource = myReportDocument;
        }
    }

Now, I got real excited, because I got a bit clever and fixed both those issues, by trapping the Navigation and keeping track of the Page myself. EVERYTHING WORKS NOW!!! until I go to the Parameter Dialog prompt and it was totally jacked up.

    // ##################################################################################################################################################
    // METHOD 3: Everything works correctly except the Prompt Box doesn't Format correcly due to the addition of the added Event Handers
    protected void Page_Load(object sender, EventArgs e)
    {

        CrystalReportViewer1.ReportSource = Session["myReportDoc"] as CrystalDecisions.CrystalReports.Engine.ReportDocument;

        if (CrystalReportViewer1.ReportSource == null)
        {
            //Generate the Report Document 
            Handlers.ReportHandler myReportHandler = new Handlers.ReportHandler();
            CrystalDecisions.CrystalReports.Engine.ReportDocument myReportDocument = myReportHandler.GenerateReport("AlarmStatusReport");
            Session["myReportDoc"] = myReportDocument;   //This is were we save it off for next time
            CrystalReportViewer1.ReportSource = myReportDocument;

            //Init our Manual Page Counter to 1
            HiddenFieldPageNumber.Value = "1";
        }

        CrystalReportViewer1.Navigate += CrystalReportViewer1_Navigate;     //Simply Adding this event, EVEN IF IT HAS NO CODE, Breaks the style and formating of the Parameter Prompt box.
        CrystalReportViewer1.PreRender += CrystalReportViewer1_PreRender;
    }

    private void CrystalReportViewer1_Navigate(object source, CrystalDecisions.Web.NavigateEventArgs e)
    {
        //This prevents this event from Incrementing the Page again when the PreRender Event 
        //below re-sets which page to show. 
        if (_SkipPageIncrement == true)
        {
            return;
        }

        //Whenever the Navigation is used, this Event fires. Here is the problem, there is nothing that actually tells 
        //us if the user clicked on Previous or Next (or GotoPage for that Matter). So we have to do some guessing here
        if (e.CurrentPageNumber == 1 && e.NewPageNumber == 2)
        {
            //If they pressed "NEXT" we will always get Current = 1 and New = 2 due to the Pagination starting over on the PostBack
            //So we INCREMENT our real Page Number Value.
            HiddenFieldPageNumber.Value = (Convert.ToInt32(HiddenFieldPageNumber.Value) + 1).ToString();
        }
        else if (e.CurrentPageNumber == 1 && e.NewPageNumber == 1)
        {
            //If they pressed "PREV" we will always get Current = 1 and New = 1 due to the Pagination starting over on the PostBack
            //So we DECREMENT our real Page Number Value.
            HiddenFieldPageNumber.Value = (Convert.ToInt32(HiddenFieldPageNumber.Value) - 1).ToString();
        }
    }

    private void CrystalReportViewer1_PreRender(object sender, EventArgs e)
    {
        //The Viewer has a method that allows us to set the page number. This PreRender Event is the only
        //Event I could find that works. It comes AFTER the Navigate, but before the reports is rendered.
        _SkipPageIncrement = true; //The ShowNthPage re-triggers the Navigation, so this prevents it from running again.
        CrystalReportViewer1.ShowNthPage(Convert.ToInt32(HiddenFieldPageNumber.Value));
    }

As commented above, the moment I add the OnNavigation Event, even if I comment out all the actual code inside, my Prompt box goes from looking like this...

在职的

To this (my page as a dark background and you can see that now shows, plus the "OK" button is all jacked up.

破碎的造型

I just don't get why trapping the Navigation Event breaks the Prompt box even when the event is not firing (on that first load).

Side note: I'm using VS 2019 with CR 13.0.3500.0

So thanks to the help of a teammate that is more adept on CSS as I am, I have resolved the issue "good enough". So for anyone who wants to use the LOAD event, (Or has to like me), but then loses the ability to use the navigation and wants to use my method, the band-aid for the Crystal Reports Parameter prompt is to simply override their Styling in you Site.css with this...

/*---------------------- Custom CSS for Report Prompt Buttons ----------------------*/

.pePromptButton {
    padding-bottom:4.3px;
}

td.pePromptButton {
    display: inherit;
}
    img {
    vertical-align:top;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM