using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
public partial class RDLC_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string ReportName = this.Request.QueryString["ReportName"] == null ? "Report1.rdlc" : this.Request.QueryString["ReportName"].ToString();
BindReportViewer(ReportName);
}
}
private void BindReportViewer(string name)
{
switch (name)
{
//传递DataSet
case "Report1.rdlc":
Microsoft.Reporting.WebForms.ReportDataSource rd = new Microsoft.Reporting.WebForms.ReportDataSource();
rd.Name = "Rdlc_rdlc";
rd.Value = SourcesDataTable();
ReportViewer1.LocalReport.ReportPath = this.MapPath(name);
ReportViewer1.LocalReport.DataSources.Add(rd);
this.ReportViewer1.LocalReport.Refresh();
break;
//传递报表参数
case "Report.rdlc":
ReportViewer1.LocalReport.ReportPath = this.MapPath(name);
ReportParameter[] parameters = new ReportParameter[1];
parameters[0] = new ReportParameter("Report_Parameter_0","asdadasd");
ReportViewer1.LocalReport.SetParameters(parameters);
this.ReportViewer1.LocalReport.Refresh();
break;
}
}
public DataTable SourcesDataTable()
{
DataTable dtSearchType = new DataTable();
dtSearchType.Columns.Add("ShuJu", typeof(int));
dtSearchType.Columns.Add("LeiBie", typeof(string));
dtSearchType.Columns.Add("XuLie", typeof(string));
dtSearchType.Rows.Add(new object[] { "88", "语文", "小小" });
dtSearchType.Rows.Add(new object[] { "99", "数学", "小小" });
dtSearchType.Rows.Add(new object[] { "70", "英语", "小小" });
dtSearchType.Rows.Add(new object[] { "88", "语文", "大大" });
dtSearchType.Rows.Add(new object[] { "70", "数学", "大大" });
dtSearchType.Rows.Add(new object[] { "90", "英语", "大大" });
dtSearchType.Rows.Add(new object[] { "88", "语文1", "小小" });
dtSearchType.Rows.Add(new object[] { "99", "数学1", "小小" });
dtSearchType.Rows.Add(new object[] { "70", "英语1", "小小" });
dtSearchType.Rows.Add(new object[] { "88", "语文1", "大大" });
dtSearchType.Rows.Add(new object[] { "70", "数学1", "大大" });
dtSearchType.Rows.Add(new object[] { "90", "英语1", "大大" });
return dtSearchType;
}
protected void Button1_Click(object sender, EventArgs e)
{
this.Response.Redirect(string.Format("Default.aspx?ReportName={0}", "Report1.rdlc"));
}
protected void Button2_Click(object sender, EventArgs e)
{
this.Response.Redirect(string.Format("Default.aspx?ReportName={0}", "Report.rdlc"));
}
}
上面实例提供下载:http://download.csdn.net/source/829843
其中由于缓存原因不能直接加载的,需要每次传递参数来更新页面.
1.http://www.cnblogs.com/Carlwave/有非常详细的实例和讲解大家可以去参考下.