ReportViewer local – rdlc – handle subreports with multiple pages and parameters

 
  1: using System;
  2: using System.Collections;
  3: using System.Configuration;
  4: using System.Data;
  5: using System.Linq;
  6: using System.Web;
  7: using System.Web.Security;
  8: using System.Web.UI;
  9: using System.Web.UI.HtmlControls;
 10: using System.Web.UI.WebControls;
 11: using System.Web.UI.WebControls.WebParts;
 12: using System.Xml.Linq;
 13: using Microsoft.Reporting.WebForms;
 14: using Rpt = SchoolDistrict.Registration.Reports; 
 15: 
 16: public partial class Reports_RegistrationReport : System.Web.UI.Page
 17: {
 18:     protected void Page_Load(object sender, EventArgs e)
 19:     {
 20:         //Workaround to allow the horizontal scroll bar to show.
 21:         //http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2803199&SiteID=1
 22:         ReportViewer1.Style.Add("margin-bottom", "26px");
 23:     }
 24:     /// <summary>
 25:     /// Event handler for the btnGenerateReport_Click event
 26:     /// </summary>
 27:     /// <param name="sender"></param>
 28:     /// <param name="e"></param>
 29:     protected void btnGenerateReport_Click(object sender, EventArgs e)
 30:     {
 31:         SetSchoolAndClassSessionKeys();
 32:         this.ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
 33:         ReportViewer1.LocalReport.Refresh();
 34:     } 
 35: 
 36:     void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
 37:     {
 38:         int schoolID;
 39:         string schoolName;
 40:         int classID;
 41:         string className;
 42:         //Converts SessionKeys to appropriate values to pass to methods
 43:         GetSchoolAndClassSessionKeys(out schoolID, out schoolName, out classID, out className); 
 44: 
 45:         //Determines which SubReport is being processed
 46:         int reportPath = GetSubReportPathCase(e); 
 47: 
 48:         switch (reportPath)
 49:         {
 50:             case 1:
 51:                 e.DataSources.Add(new ReportDataSource("Registration", new Rpt.ReportManager().GetRegistrationRecordPctBySchool(
 52:                     schoolID, schoolName, classID, className)));
 53:                 break;
 54:             case 2:
 55:                 e.DataSources.Add(new ReportDataSource("Registration", new Rpt.ReportManager().GetRegistrationRecordPctBySchoolDistrict(
 56:                     schoolID, schoolName, classID, className)));
 57:                 break;
 58:             case 3:
 59:                 e.DataSources.Add(new ReportDataSource("Registration", new Rpt.ReportManager().GetRegistrationRecordComments(
 60:                     schoolID, schoolName, classID, className)));
 61:                 break;
 62:             default:
 63:                 break;
 64:         }
 65:         ////Guru's code
 66:         //e.DataSources.Add(new ReportDataSource("Registration", new Rpt.ReportManager().GetRegistrationRecord(
 67:         //    int.Parse(Session["SchoolNameKey"].ToString()),
 68:         //    Session["SchoolName"] as string, int.Parse(Session["ClassNameKey"].ToString()),
 69:         //    Session["ClassName"] as string))); 
 70: 
 71:         //throw new NotImplementedException();
 72:         //ReportViewer1.LocalReport.Render("PDF",
 73:     } 
 74: 
 75:     /// <summary>
 76:     /// Determines which SubReport is being processed
 77:     /// </summary>
 78:     /// <param name="e"></param>
 79:     /// <returns></returns>
 80:     private static int GetSubReportPathCase(SubreportProcessingEventArgs e)
 81:     {
 82:         //Determines which SubReport is being processed
 83:         int reportPath = 0; 
 84: 
 85:         if (e.ReportPath == "PctBySchool")
 86:         {
 87:             reportPath = 1;
 88:         }
 89:         else if (e.ReportPath == "PctBySchoolDistrict")
 90:         {
 91:             reportPath = 2;
 92:         }
 93:         else if (e.ReportPath == "Comments")
 94:         {
 95:             reportPath = 3;
 96:         }
 97:         else
 98:         {
 99:         }
100:         return reportPath;
101:     } 
102: 
103:     /// <summary>
104:     /// Converts SessionKeys to appropriate values to pass to methods
105:     /// </summary>
106:     /// <param name="schoolID">passed in as 0</param>
107:     /// <param name="schoolName">passed in as empty string</param>
108:     /// <param name="classID">passed in as 0</param>
109:     /// <param name="className">passed in as empty string</param>
110:     private void GetSchoolAndClassSessionKeys(out int schoolID, out string schoolName, out int classID, out string className)
111:     {
112:         //Converts SessionKeys to appropriate values to pass to methods
113:         int badValue; 
114: 
115:         if (int.TryParse(Session["SchoolNameKey"].ToString(), out badValue))
116:         {
117:             schoolID = int.Parse(Session["SchoolNameKey"].ToString());
118:         }
119:         else
120:         {
121:             schoolID = 0;
122:         } 
123: 
124:         schoolName = Session["SchoolName"].ToString(); 
125: 
126:         if (int.TryParse(Session["ClassNameKey"].ToString(), out badValue))
127:         {
128:             classID = int.Parse(Session["ClassNameKey"].ToString());
129:         }
130:         else
131:         {
132:             classID = 0;
133:         } 
134: 
135:         className = Session["ClassName"].ToString();
136:     } 
137: 
138:     /// <summary>
139:     /// Method to Set or Reset the Session Keys for the School/Class based on the values in two
140:     /// Ajax Cascading Drop-Down Lists.
141:     /// </summary>
142:     private void SetSchoolAndClassSessionKeys()
143:     {
144:         if (!((string.IsNullOrEmpty(ddlSchoolName.SelectedValue)) || (string.IsNullOrEmpty(ddlClassName.SelectedValue))))
145:         {
146:             Session[ReportKeys.SchoolNameID] = ddlSchoolName.SelectedValue;
147:             Session[ReportKeys.SchoolName] = ddlSchoolName.SelectedItem.ToString();
148:             Session[ReportKeys.ClassNameID] = ddlClassName.SelectedValue;
149:             Session[ReportKeys.ClassName] = ddlClassName.SelectedItem.ToString(); 
150: 
151:         }
152:         else
153:         {
154:             Session[ReportKeys.SchoolNameID] = string.Empty;
155:             Session[ReportKeys.SchoolName] = string.Empty;
156:             Session[ReportKeys.ClassNameID] = string.Empty;
157:             Session[ReportKeys.ClassName] = string.Empty; 
158: 
159:             //ReportViewer1.LocalReport.Refresh();
160:         }
161:     } 
162: 
163:     /// <summary>
164:     /// Event Handler for the ReportViewer1 Refresh Button
165:     /// </summary>
166:     /// <param name="sender"></param>
167:     /// <param name="e"></param>
168:     protected void ReportViewer1_ReportRefresh(object sender, System.ComponentModel.CancelEventArgs e)
169:     {
170:         btnGenerateReport_Click(sender, e);
171:     }
172: } 
173: 
And Voila!
Technorati Tags: ,,,,,,

Print | posted on Friday, October 9, 2009 12:46 AM

Feedback

No comments posted yet.

Your comment:





 
 

Copyright © intermark

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski