Need help for using Crystal report viewer in ASP.Net 2.0

Last post 11-05-2009 1:31 PM by ssstars. 3 replies.

Sort Posts:

  • Need help for using Crystal report viewer in ASP.Net 2.0

    06-26-2007, 12:50 PM

    Hi All,

     I'm new to ASP.Net, and I was trying to create a web site, with crystal reports.

     
    I created the reports, added them to my pages, and they are working fine.

    the problem is when I try to  navigate through the report pages, or print or export the page is reloaded "postback", I need to stop this action.

    Some of my told me that I can use AJAX, but I do not how.

    can anybody give me an example for how to use crystal report viewer in ASP.Net 2.0 with AJAX.

     
    Thanks in advance.

    Have a nice day.
     

    Regards.
  • Re: Need help for using Crystal report viewer in ASP.Net 2.0

    06-26-2007, 2:41 PM
    • Participant
      1,186 point Participant
    • rstepan
    • Member since 03-27-2006, 3:51 PM
    • Posts 204

     

    http://forums.asp.net/thread/1325110.aspx
    (UpdatePanel does not allow the CrystalReportViewer control to print or export.)
    
    http://forums.asp.net/thread/1660467.aspx
    (button inside update panel Firefox problem)
    I guess we'll have to wait until Business Objects decides to make changes to CrystalReprotViewer. Until then Postbacks are a neccessary evil!!
    
     
  • Re: Need help for using Crystal report viewer in ASP.Net 2.0

    08-15-2007, 4:21 PM
    • Member
      14 point Member
    • meehanp
    • Member since 08-01-2007, 9:22 PM
    • Houston, TX
    • Posts 2

    This answer is a bit late for you, but perhaps will help someone else.

     It is true, you can suppress the postbacks generated by the CR viewer control by placing them in an AJAX updatepanel control, but then you can't use the built in print/export functions.  You can enable the print/export functions of the viewer in an updatepanel, but then you will also get postbacks when you navigate the report.  You can't do both here. 

     The answer for you I think is how to handle the postbacks.  We use the following method for almost all of our reports and it seems to work fine.  Although we are tucking the data into the session object so this solution might not scale well if it is used for a large scale reporting solution.

    Typically, we design our pages with a couple of textboxes to allow the user to input a date range and then include a retrieve button.  When the page is first rendered, the viewer's visible property is false.  Once the user clicks retrieve, then the report generates the data.  You could skip this part if there are no user specific paramenters.  In that case you would put the report generating logic within the Page.IsPostBack = False portion of the If..Then..Else block.  This particular example uses both an UpdatePanel control and a UpdateProgress control.  Also, we are using CrystalReports XI r2.  If you are using an earlier version I highly recommend upgrading (and no I don't work for Business Objects)  They significanly simplified things with the CrystalReportSource control. The following is the source of the aspx page.:

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="LeadSummary.aspx.vb" Inherits="LeadSummary" %>

    <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    Namespace="System.Web.UI" TagPrefix="asp" %>

    <%@ Register Assembly="CrystalDecisions.Web, Version=11.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"

    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head id="Head1" runat="server">

    <title>Internet Leads Summary Report</title>

    </head><body><form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptLocalization="True"></asp:ScriptManager>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>

    <asp:Panel ID="Panel1" runat="server" Height="80px" Style="z-index: 100; left: 16px; position: absolute; top: 24px" Width="656px">

    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Style="z-index: 100;left: 488px; position: absolute; top: 16px" Width=80px Text="Retrieve" />

    <asp:TextBox ID="txtStartDate" runat="server" Style="z-index: 101; left: 80px; position: absolute; top: 16px"></asp:TextBox>

    <asp:TextBox ID="txtEndDate" runat="server" Style="z-index: 102; left: 320px; position: absolute; top: 16px"></asp:TextBox>

    <asp:Label ID="Label2" runat="server" Style="z-index: 103; left: 8px; position: absolute; top: 16px" Text="Start Date"></asp:Label>

    <asp:Label ID="Label3" runat="server" Style="z-index: 104; left: 256px; position: absolute; top: 16px" Text="End Date"></asp:Label>

    <asp:CheckBox ID="chkDetails" runat="server" Style="z-index: 106; left: 48px; position: absolute; top: 48px" Text="Show Project Details" />

    </asp:Panel>

    <asp:Panel ID="Panel2" runat="server" Height="344px" Style="z-index: 102; left: 8px; position: absolute; top: 112px" Width="656px">

    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" Style="z-index: 64; left: 0px; position: absolute; top: 0px" ReportSourceID="CrystalReportSource1" Visible="False" />

    <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">

    <Report FileName="InternetLeadSummary.rpt"></Report>

    </CR:CrystalReportSource>

    </asp:Panel>

    </ContentTemplate>

    </asp:UpdatePanel>

    <asp:UpdateProgress ID="UpdateProgress1" runat="server">

    <ProgressTemplate>

    <img src="images/ajax-loader.gif" style="z-index: 100; left: 248px; position: absolute; top: 72px" width="16" height="16" />

    &nbsp;

    <asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 280px; position: absolute; top: 72px" Text="Retrieving data from Sales Solution" Width="304px"></asp:Label>

    </ProgressTemplate>

    </asp:UpdateProgress>

    </form></body></html>

    The magic part of making this work is the line Me.ScriptManager1.RegisterPostBackControl(Me.CrystalReportViewer1) in the Page.Load event.  This tells AJAX to treat the CrystalReports viewer control like a "Non-AJAX" control even though it is in an UpatePanel.  This allows the postback events from the viewer fire and lets you use the print/export.  It will still cause a postback during navigation, but because of the way the event handlers work with the AJAX, nothing will acutally happen except the rebuilding of the dataset.  Also note that InetLeadData is a Typed Dataset with a table called SummaryReport.

    This is the code from the code behind file:

    Partial Class LeadSummary

    Inherits System.Web.UI.Page

    Private ReportData As InetLeadData

    Private StartDate As DateTime

    Private EndDate As DateTime

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Me.ScriptManager1.RegisterPostBackControl(Me.CrystalReportViewer1)

    If Not Page.IsPostBack Then

    Me.txtStartDate.Text = String.Format("{0}/1/{1}", Month(Now()).ToString, Year(Now()).ToString)

    Me.txtEndDate.Text = Now().ToShortDateString

    Else

    If Session("Data") IsNot Nothing Then

    ReportData = CType(Session("Data"), InetLeadData)

    End If

    End If

    End Sub

    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs)

    ReportData = New InetLeadData

    Try

    StartDate = CDate(Me.txtStartDate.Text)

    EndDate = CDate(Me.txtEndDate.Text & " 11:59:59 PM")

    Catch ex As Exception

    End Try

    Dim da As New InetLeadDataTableAdapters.SummaryReportTableAdapter

    da.Fill(ReportData.SummaryReport, StartDate, EndDate)

    Me.CrystalReportSource1.ReportDocument.SetDataSource(ReportData.Tables("SummaryReport"))

    Me.CrystalReportSource1.ReportDocument.SetParameterValue("ShowDetails", Me.chkDetails.Checked)

    Me.CrystalReportSource1.ReportDocument.SetParameterValue("Start", StartDate)

    Me.CrystalReportSource1.ReportDocument.SetParameterValue("End", EndDate)

    Me.CrystalReportViewer1.Visible = True

     

    End Sub

    End Class

    Patrick Meehan
    MCAD
    Sr. Systems Analyst, Perry Homes
  • Re: Need help for using Crystal report viewer in ASP.Net 2.0

    11-05-2009, 1:31 PM
    • Member
      238 point Member
    • ssstars
    • Member since 01-11-2009, 1:21 AM
    • Posts 98

    Hello,

    Yes, you posted your "solution" a long time after the original post ...  with the intention of helping someone.   Well, I am taking the time to respond to let you know that your post solved my problem today (approx 2 years later). 

    Thank you very much.

    I was facing the same problem with "unable to print/export".   One line of your code made all the difference... Me.ScriptManager1.RegisterPostBackControl(Me.CrystalReportViewer1). Also, my CrystalReportViewer1 is "visible" during pageload.

    Just want to add that in my case I am using: asp.net 3.5; C# and  crystal reports 2008.   You would think that perhaps this problem was fixed with this new release of crysal reports 2008 (compared to the version you used).

    Hope this thread contines to help others.

    Thanks again.

Page 1 of 1 (4 items)