Share via


逐步解說:在本機處理模式中,搭配 ReportViewer Web 伺服器控制項使用商務物件資料來源

本逐步解說會示範如何在 Microsoft Visual Studio ASP.NET 應用程式中使用報表內的物件資料來源。如需有關商務物件和物件資料來源的詳細資訊,請參閱Binding to Business Objects

請執行下列步驟,將報表加入 ASP.NET 網站專案中。在這個範例中,您將在 Microsoft Visual C# 中建立應用程式。

建立新的 ASP.NET 網站專案

  1. 在 [檔案] 功能表中,指向 [新增],選取 [網站]。

  2. 在 [新網站] 對話方塊中,選取 [已安裝的範本] 窗格中的 [C#],然後選擇 [ASP.NET 網站]。根據 Visual Studio 中的啟動設定,C# 節點可能位於 [其他語言] 底下。

  3. 在 [位置] 方塊中,指定專案目錄,然後按一下 [確定]。

    網站專案隨即開啟。

建立商務物件來做為資料來源使用。

  1. 選取 [網站] 功能表上的 [加入新項目]。

  2. 在 [加入新項目] 對話方塊中,選擇 [類別],輸入 BusinessObjects.cs 做為檔案名稱,再按一下 [新增]。

  3. 按一下 [],將此類別放在 'App_Code' 資料夾中。此時會將新檔案加入專案中,並在 Visual Studio 中自動開啟它。

  4. 利用下列程式碼來取代 BusinessObjects.cs 的預設程式碼:

    using System;
    using System.Collections.Generic;
    
    // Define the Business Object "Product" with two public properties
    //    of simple datatypes.
    public class Product {
        private string m_name;
        private int m_price;
    
        public Product(string name, int price) {
            m_name = name;
            m_price = price;
        }
    
        public string Name {
            get {
                return m_name;
            }
        }
    
        public int Price {
            get {
                return m_price;
            }
        }
    }
    
    // Define Business Object "Merchant" that provides a 
    //    GetProducts method that returns a collection of 
    //    Product objects.
    
    public class Merchant {
        private List<Product> m_products;
    
        public Merchant() {
            m_products = new List<Product>();
            m_products.Add(new Product("Pen", 25));
            m_products.Add(new Product("Pencil", 30));
            m_products.Add(new Product("Notebook", 15));
        }
    
        public List<Product> GetProducts() {
            return m_products;
        }
    }
    
  5. 請從 [建置] 功能表上,選取 [建置方案]。這會建立物件的組件,在您將報表加入專案之後,可讓商務物件 [產品] 出現在 [報表資料] 視窗中。

使用報表精靈將報表加入至專案

  1. 確認在 [方案總管] 中已選取最上層網站。

  2. 以滑鼠右鍵按一下網站,選取 [加入新項目]。

  3. 在 [加入新項目] 對話方塊中,選取 [報表精靈]。請輸入報表的名稱,再按一下 [新增]。

  4. 這會啟動 [報表精靈]。

  5. 在 [資料集屬性] 頁面中,選取 [資料來源] 方塊中的 [全域]。

  6. 確認已選取 [可用資料集] 方塊中的 [Merchant (GetProducts)]。

  7. 按 [下一步]。

  8. 在 [排列欄位] 頁面中,執行下列動作:

    1. 將 [名稱] 從 [可用的欄位] 拖曳至 [資料列群組] 方塊。

    2. 將 [Price] 從 [可用的欄位] 拖曳至 [] 方塊。

  9. 按 [下一步] 兩次,然後按一下 [完成]。

    這會建立 .rdlc 檔案並在報表設計工具中開啟檔案。您所建立的 Tablix 現在會顯示在設計介面中。

  10. 儲存 .rdlc 檔案。

將 ReportViewer 控制項加入網頁中

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 Web Form Default.aspx,然後選取 [檢視表設計工具]。

  2. 開啟 [工具箱] 視窗。將 ScriptManager 控制項從 [AJAX 擴充功能] 群組拖曳至設計介面。

  3. ReportViewer 控制項從 [工具箱] 的 [報告] 群組拖曳至網頁的 ScriptManager 控制項下方。

  4. 選取 ReportViewer 控制項中,按一下右上角的智慧標籤圖像來開啟智慧標籤面板。

  5. 在 [選擇報表] 清單中選取您剛設計的報表。請注意,ObjectDataSource 控制項會緊接在 ReportViewer 控制項的下方出現,而且會自動設定為可擷取您所建立商務物件的內容。

執行應用程式

  • F5 鍵在偵錯模式下執行,或是按 CTRL + F5 在非偵錯模式下執行,並檢視報表。

請參閱

參考

ReportViewer.Drillthrough

LocalReport.SubreportProcessing

ReportViewer.Drillthrough

LocalReport.SubreportProcessing

概念

使用 [ReportViewer 工作] 智慧標籤面板

其他資源

範例和逐步解說