摘要:ASP.NET - ReportViewer Control 使用心得 (三)
在第二篇<ASP.NET - ReportViewer Control 使用心得 (二)>中是使用 伺服器總管 的方式連線到 Northwind,之後再建立 DataSet 將 Northwind 的資料表拖拉到 DataSet 當中。這一次將使用另一種做法來完成,其手法類似,但個人覺得會比第二篇要來得簡單...
步驟一:請在 Web.config 中加入 Northwind 的連線字串
Code:
<connectionStrings>
<add name="NorthwindConnectionString"
connectionString="Data Source=[DB Server];
Initial Catalog=[DB Name];Persist Security Info=True;
User ID=[Account];Password=[password]" providerName="System.Data.SqlClient"/>
</connectionStrings>
步驟二:建立一個 報表 檔
步驟三:使用 報表資料來源 來建立 新的資料來源
步驟四:在 DataSet1.xsd 中加入 TableAdapter
步驟五:回 Report1.rdlc,在 工具箱 拉進 資料表,並且將相關欄位就定完成
步驟六:到 Default.aspx 中佈置畫面,並將 DropDownList 的 AutoPostBack 設為 啟用
步驟七:設定 ReportViewer 的 選擇報表
步驟八:撰寫程式碼
Code:
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using Microsoft.Reporting.WebForms;
namespace ReportViewerSampleTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ReportViewer1.Visible = false;
SetDropDownList();
}
}
private void SetDropDownList()
{
using (SqlConnection cn = new SqlConnection(WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString))
{
DataSet ds = new DataSet();
SqlCommand cmd =
new SqlCommand("Select CustomerID From Orders Group By CustomerID Order By CustomerID", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "GroupCustomerID");
DropDownList1.DataSource = ds.Tables["GroupCustomerID"];
DropDownList1.DataValueField = "CustomerID";
DropDownList1.DataTextField = "CustomerID";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ObjectDataSource1.SelectParameters.Clear();
ObjectDataSource1.SelectMethod = "GetData";
ObjectDataSource1.SelectParameters.Add(new Parameter("CustomerID", DbType.String, DropDownList1.SelectedValue));
ReportDataSource rds = new ReportDataSource("DataSet1_Orders", ObjectDataSource1.ID);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();
ReportViewer1.Visible = true;
}
}
}
前端的程式碼
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ReportViewerSampleTest._Default" %>
<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"
Font-Size="8pt" Height="400px" Width="689px">
<LocalReport ReportPath="Report1.rdlc">
<DataSources>
<rsweb:ReportDataSource DataSourceId="ObjectDataSource1"
Name="DataSet1_Orders" />
</DataSources>
</LocalReport>
</rsweb:ReportViewer>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetData"
TypeName="ReportViewerSampleTest.DataSet1TableAdapters.OrdersTableAdapter">
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
結果:
補充一點:
這幾篇所實作的都是很簡單的範例,所以以下的設定請依據當時專案的實際需求來評估是否可以使用...
介於 步驟七 與 步驟八 之間的步驟,是否多餘看個人的想法嚕...
補充步驟一:設定 ObjectDataSource
補充步驟二:修改 DropDownList1_SelectedIndexChanged 中的程式碼
Code:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ReportViewer1.LocalReport.Refresh();
ReportViewer1.Visible = true;
}