Utilizzo del controllo Web Form ReportViewerUsing the WebForms ReportViewer Control

Per visualizzare i report distribuiti in un server di report o presenti in un file system locale, è possibile utilizzare il controllo Web Form ReportViewer per eseguirne il rendering in un'applicazione Web.To view reports deployed to a report server or reports that exist on the local file system, you can use the WebForms ReportViewer control to render them in a Web application.

Per utilizzare il controllo ReportViewer in un'applicazione WebTo use the ReportViewer Control in a Web application

  1. Creare un nuovo MicrosoftMicrosoft sito Web ASP.NET utilizzando MicrosoftMicrosoft Visual C#Visual C# o MicrosoftMicrosoft Visual BasicVisual Basic.Create a new MicrosoftMicrosoft ASP.NET Web Site using either MicrosoftMicrosoft Visual C#Visual C# or MicrosoftMicrosoft Visual BasicVisual Basic.

    -O -- Or -

    Aprire un sito Web ASP.NET esistente e aggiungere un nuovo Web Form.Open an exiting ASP.NET Web Site and add a new Web Form.

  2. Individuare il ScriptManager controllo il Estensioni AJAX gruppo il della casella degli strumenti finestra e trascinarlo nell'area di progettazione del Web form.Locate the ScriptManager control in the AJAX Extensions group in the Toolbox window, and drag it to the design surface of the Web form.

    Se il della casella degli strumenti è non è visibile, è possibile accedervi dal vista menu selezionando della casella degli strumenti.If the Toolbox is not visible, you can access it from the View menu by selecting Toolbox.

  3. Individuare il ReportViewer controllo il della casella degli strumenti e trascinarlo nell'area di progettazione sotto la ScriptManager controllo.Locate the ReportViewer control in the Toolbox and drag it to the design surface below the ScriptManager control.

    Il ReportViewer controllo denominato reportViewer1 verrà aggiunto al form.The ReportViewer control named reportViewer1 is added to the form.

    Dopo il controllo viene aggiunto al form il attività di ReportViewer viene visualizzato smart tag che richiede di selezionare un report.After the control is added to the form the ReportViewer Tasks smart tag will appear prompting you to select a report. Se il report che si desidera visualizzare è stato distribuito a un server di report selezionare il <Report Server > opzione il Scegli Report elenco a discesa.If the report you wish to view has been deployed to a report server select the <Server Report> option from the Choose Report drop-down list. Una volta il <Report Server > opzione è selezionata due proprietà aggiuntive verranno visualizzate, Url Server di Report e percorso Report.Once the <Server Report> option is selected two additional properties will appear, Report Server Url and Report Path. Il Url Server di Report è l'indirizzo del server di report e percorso Report è il percorso completo del report che si desidera eseguire il rendering.The Report Server Url is the address to the report server and the Report Path is the full path to the report you want to render.

    Se si desidera visualizzare un report in modalità locale, è possibile selezionare l'opzione di Progetta nuovo report opzione per avviare Progettazione report oppure selezionare un report che fa già parte del progetto esistente.If you want to view a report in local mode select either the Design a new report option to launch the report designer or select a report that is already part of the existing project. Dopo aver selezionato un report, assicurarsi di immettere il nome del file RDLC del report nel ReportPath proprietà del controllo ReportViewer.After you have selected a report, be sure to enter the name of the report RDLC file in the ReportPath property of the ReportViewer control. Questa proprietà viene visualizzata sotto il LocalReport nodo il proprietà riquadro.This property appears under the LocalReport node in the Properties pane.

    È possibile nascondere uno o più elementi sulla barra degli strumenti di ReportViewer quando viene eseguito il rendering del report.You have the option of hiding one or more of the items on the ReportViewer toolbar when the report is rendered. Ad esempio, è possibile nascondere il pulsante di stampa.For example, you can hide the print button. Per nascondere gli elementi della barra degli strumenti, impostare le proprietà seguenti di ReportViewer False nel proprietà riquadro.To hide toolbar items, set the following ReportViewer properties to False in the Properties pane.

  • ShowBackButtonShowBackButton

  • ShowExportControlsShowExportControls

  • ShowFindControlsShowFindControls

  • ShowPageNavigationControlsShowPageNavigationControls

  • ShowPrintButtonShowPrintButton

  • ShowRefreshButtonShowRefreshButton

  • ShowZoomControlShowZoomControl

Visualizzazione di report in modalità di elaborazione remotaViewing Reports in Remote Processing Mode

Nell'esempio seguente viene illustrato come eseguire il rendering di un report distribuito in un server di report.The following example demonstrates how to render a report that has been deployed to a report server. In questo esempio viene usato il report Sales Order Detail incluso nel progetto di report di esempio AdventureWorks2012AdventureWorks2012.This example uses the Sales Order Detail report that is included with the AdventureWorks2012AdventureWorks2012 sample reports project.

Nell'esempio viene utilizzata l'autenticazione integrata di Windows, pertanto è innanzitutto necessario abilitare la rappresentazione.The example uses integrated Windows Authentication so you first must enable impersonation. Per eseguire questo inserire la riga seguente nel Web. config file:To do this insert the following line into the web.config file:

<!-- Web.config file. -->  
<identity impersonate="true"/>  
Nota

Per impostazione predefinita, la rappresentazione è disabilitata.Impersonation is disabled by default.

LINGUAGGIO C#C#

protected void Page_Init(object sender, EventArgs e)  
{  
    if (!Page.IsPostBack)  
    {  
        // Set the processing mode for the ReportViewer to Remote  
        reportViewer.ProcessingMode = ProcessingMode.Remote;  

        ServerReport serverReport = reportViewer.ServerReport;  

        // Set the report server URL and report path  
        serverReport.ReportServerUrl =  
            new Uri("http://<Server Name>/reportserver");  
        serverReport.ReportPath =  
            "/AdventureWorks Sample Reports/Sales Order Detail";  

        // Create the sales order number report parameter  
        ReportParameter salesOrderNumber = new ReportParameter();  
        salesOrderNumber.Name = "SalesOrderNumber";  
        salesOrderNumber.Values.Add("SO43661");  

        // Set the report parameters for the report  
        reportViewer.ServerReport.SetParameters(  
            new ReportParameter[] { salesOrderNumber });  
    }  
}  

VISUAL BASIC.NETVB.NET

Imports Microsoft.Reporting.WebForms  

Partial Class _Default  
    Inherits System.Web.UI.Page  

    Protected Sub Page_Init(ByVal sender As Object, _  
            ByVal e As System.EventArgs) Handles Me.Init  

        If Not Page.IsPostBack Then  

            'Set the processing mode for the ReportViewer to Remote  
            reportViewer.ProcessingMode = ProcessingMode.Remote  

            Dim serverReport As ServerReport  
            serverReport = reportViewer.ServerReport  

            'Set the report server URL and report path  
            serverReport.ReportServerUrl = _  
                New Uri("http://<Server Name>/reportserver")  
            serverReport.ReportPath = _  
                "/AdventureWorks Sample Reports/Sales Order Detail"  

            'Create the sales order number report parameter  
            Dim salesOrderNumber As New ReportParameter()  
            salesOrderNumber.Name = "SalesOrderNumber"  
            salesOrderNumber.Values.Add("SO43661")  

            'Set the report parameters for the report  
            Dim parameters() As ReportParameter = {salesOrderNumber}  
            serverReport.SetParameters(parameters)  

        End If  

    End Sub  

End Class  

Visualizzazione di report in modalità di elaborazione localeViewing Reports in Local Processing Mode

Nell'esempio seguente viene illustrato come eseguire il rendering di un report che fa parte di un'applicazione Windows e non è stato distribuito in un server di report.The following example demonstrates how to render a report that is part of the Windows application and has not been deployed to a report server.

Per aggiungere il report Sales Order Detail a un sito WebTo add the Sales Order Detail report to a Web Site
  1. Aprire il sito Web a cui verrà aggiunto il report.Open the Web Site that the report will be added to.

  2. Dal sito Web dal menu Aggiungi elemento esistente.From the Website menu, select Add Existing Item.

  3. Passare al percorso di installazione del progetto di report di esempio AdventureWorks.Browse to the location where the AdventureWorks Report Samples project is installed.

    Il percorso predefinito è C:\Programmi\Microsoft SQL Server\100\Samples\Reporting Services\Report Samples\AdventureWorks Sample Reports.The default location is C:\Program Files\Microsoft SQL Server\100\Samples\Reporting Services\Report Samples\AdventureWorks Sample Reports.

  4. Selezionare il file Sales Order Detail.rdl e fare clic su di Aggiungi pulsante.Select the Sales Order Detail.rdl file and click the Add button.

    Il file Sales Order Detail.rdl dovrebbe ora far parte del progetto.The Sales Order Detail.rdl file should now be part of the project.

  5. Il file Sales Order Detail.rdl in Esplora soluzioni e selezionare rinominare.Right-click the Sales Order Detail.rdl file in Solution Explorer and select Rename. Rinominare il report in Sales Order rdlc e premere INVIO.Rename the report to Sales Order Detail.rdlc and press ENTER.

    Se Esplora soluzioni non è visibile, è possibile aprirlo dal vista menu scegliendo Esplora soluzioni.If Solution Explorer is not visible, you can open it from the View menu by selecting Solution Explorer.

    Nell'esempio di codice seguente viene creato un set di dati per i dati dell'ordine di vendita, quindi viene eseguito il rendering del report Sales Order Detail in modalità locale.The following code example will create a dataset for the sales order data and then render the Sales Order Detail report in local mode.

LINGUAGGIO C#C#

protected void Page_Init(object sender, EventArgs e)  
{  
    if (!Page.IsPostBack)  
    {  
        // Set the processing mode for the ReportViewer to Local  
        reportViewer.ProcessingMode = ProcessingMode.Local;  

        LocalReport localReport = reportViewer.LocalReport;  

        localReport.ReportPath = "Sales Order Detail.rdlc";  

        DataSet dataset = new DataSet("Sales Order Detail");  

        string salesOrderNumber = "SO43661";  

        GetSalesOrderData(salesOrderNumber, ref dataset);  

        ReportDataSource dsSalesOrder = new ReportDataSource();  
        dsSalesOrder.Name = "SalesOrder";  
        dsSalesOrder.Value = dataset.Tables["SalesOrder"];  

        localReport.DataSources.Add(dsSalesOrder);  

        GetSalesOrderDetailData(salesOrderNumber, ref dataset);  

        ReportDataSource dsSalesOrderDetail = new ReportDataSource();  
        dsSalesOrderDetail.Name = "SalesOrderDetail";  
        dsSalesOrderDetail.Value = dataset.Tables["SalesOrderDetail"];  

        localReport.DataSources.Add(dsSalesOrderDetail);  

        // Create the sales order number report parameter  
        ReportParameter rpSalesOrderNumber = new ReportParameter();  
        rpSalesOrderNumber.Name = "SalesOrderNumber";  
        rpSalesOrderNumber.Values.Add("SO43661");  

        // Set the report parameters for the report  
        localReport.SetParameters(  
            new ReportParameter[] { rpSalesOrderNumber });  
    }  
}  

private void GetSalesOrderData(string salesOrderNumber,  
                           ref DataSet dsSalesOrder)  
{  
    string sqlSalesOrder =  
        "SELECT SOH.SalesOrderNumber, S.Name AS Store, " +  
        "       SOH.OrderDate, C.FirstName AS SalesFirstName, " +  
        "       C.LastName AS SalesLastName, E.Title AS " +  
        "       SalesTitle, SOH.PurchaseOrderNumber, " +  
        "       SM.Name AS ShipMethod, BA.AddressLine1 " +  
        "       AS BillAddress1, BA.AddressLine2 AS " +  
        "       BillAddress2, BA.City AS BillCity, " +  
        "       BA.PostalCode AS BillPostalCode, BSP.Name " +  
        "       AS BillStateProvince, BCR.Name AS " +  
        "       BillCountryRegion, SA.AddressLine1 AS " +  
        "       ShipAddress1, SA.AddressLine2 AS " +  
        "       ShipAddress2, SA.City AS ShipCity, " +  
        "       SA.PostalCode AS ShipPostalCode, SSP.Name " +  
        "       AS ShipStateProvince, SCR.Name AS " +  
        "       ShipCountryRegion, CC.Phone AS CustPhone, " +  
        "       CC.FirstName AS CustFirstName, CC.LastName " +  
        "       AS CustLastName " +  
        "FROM   Person.Address SA INNER JOIN " +  
        "       Person.StateProvince SSP ON " +  
        "       SA.StateProvinceID = SSP.StateProvinceID " +  
        "       INNER JOIN Person.CountryRegion SCR ON " +  
        "       SSP.CountryRegionCode = SCR.CountryRegionCode " +  
        "       RIGHT OUTER JOIN Sales.SalesOrderHeader SOH " +  
        "       LEFT OUTER JOIN  Person.Contact CC ON " +  
        "       SOH.ContactID = CC.ContactID LEFT OUTER JOIN" +  
        "       Person.Address BA INNER JOIN " +  
        "       Person.StateProvince BSP ON " +  
        "       BA.StateProvinceID = BSP.StateProvinceID " +  
        "       INNER JOIN Person.CountryRegion BCR ON " +  
        "       BSP.CountryRegionCode = " +  
        "       BCR.CountryRegionCode ON SOH.BillToAddressID " +  
        "       = BA.AddressID ON  SA.AddressID = " +  
        "       SOH.ShipToAddressID LEFT OUTER JOIN " +  
        "       Person.Contact C RIGHT OUTER JOIN " +  
        "       HumanResources.Employee E ON C.ContactID = " +  
        "       E.ContactID ON SOH.SalesPersonID = " +  
        "       E.EmployeeID LEFT OUTER JOIN " +  
        "       Purchasing.ShipMethod SM ON SOH.ShipMethodID " +  
        "       = SM.ShipMethodID LEFT OUTER JOIN Sales.Store" +  
        "        S ON SOH.CustomerID = S.CustomerID " +  
        "WHERE  (SOH.SalesOrderNumber = @SalesOrderNumber)";  

    SqlConnection connection = new  
        SqlConnection("Data Source=(local); " +  
                      "Initial Catalog=AdventureWorks; " +  
                      "Integrated Security=SSPI");  

    SqlCommand command =  
        new SqlCommand(sqlSalesOrder, connection);  

    command.Parameters.Add(  
        new SqlParameter("SalesOrderNumber",  
        salesOrderNumber));  

    SqlDataAdapter salesOrderAdapter = new  
        SqlDataAdapter(command);  

    salesOrderAdapter.Fill(dsSalesOrder, "SalesOrder");  
}  

private void GetSalesOrderDetailData(string salesOrderNumber,  
                       ref DataSet dsSalesOrder)  
{  
    string sqlSalesOrderDetail =  
        "SELECT  SOD.SalesOrderDetailID, SOD.OrderQty, " +  
        "        SOD.UnitPrice, CASE WHEN " +  
        "        SOD.UnitPriceDiscount IS NULL THEN 0 " +  
        "        ELSE SOD.UnitPriceDiscount END AS " +  
        "        UnitPriceDiscount, SOD.LineTotal, " +  
        "        SOD.CarrierTrackingNumber, " +  
        "        SOD.SalesOrderID, P.Name, P.ProductNumber " +  
        "FROM    Sales.SalesOrderDetail SOD INNER JOIN " +  
        "        Production.Product P ON SOD.ProductID = " +  
        "        P.ProductID INNER JOIN " +  
        "        Sales.SalesOrderHeader SOH ON " +  
        "        SOD.SalesOrderID = SOH.SalesOrderID " +  
        "WHERE   (SOH.SalesOrderNumber = @SalesOrderNumber) " +  
        "ORDER BY SOD.SalesOrderDetailID";  

    using (SqlConnection connection = new  
        SqlConnection("Data Source=(local); " +  
                      "Initial Catalog=AdventureWorks; " +  
                      "Integrated Security=SSPI"))  
    {  

        SqlCommand command =  
            new SqlCommand(sqlSalesOrderDetail, connection);  

        command.Parameters.Add(  
            new SqlParameter("SalesOrderNumber",  
            salesOrderNumber));  

        SqlDataAdapter salesOrderDetailAdapter = new  
            SqlDataAdapter(command);  

        salesOrderDetailAdapter.Fill(dsSalesOrder,  
            "SalesOrderDetail");  
    }  
}  

VISUAL BASIC.NETVB.NET

Imports System.Data  
Imports System.Data.SqlClient  
Imports Microsoft.Reporting.WebForms  

Partial Class _Default  
    Inherits System.Web.UI.Page  

    Protected Sub Page_Init(ByVal sender As Object, _  
                ByVal e As System.EventArgs) Handles Me.Init  

        If Not Page.IsPostBack Then  

            'Set the processing mode for the ReportViewer to Local  
            reportViewer.ProcessingMode = ProcessingMode.Local  

            Dim localReport As LocalReport  
            localReport = reportViewer.LocalReport  

            localReport.ReportPath = "Sales Order Detail.rdlc"  

            Dim dataset As New DataSet("Sales Order Detail")  

            Dim salesOrderNumber As String = "SO43661"  

            'Get the sales order data  
            GetSalesOrderData(salesOrderNumber, dataset)  

            'Create a report data source for the sales order data  
            Dim dsSalesOrder As New ReportDataSource()  
            dsSalesOrder.Name = "SalesOrder"  
            dsSalesOrder.Value = dataset.Tables("SalesOrder")  

            localReport.DataSources.Add(dsSalesOrder)  

            'Get the sales order detail data  
            GetSalesOrderDetailData(salesOrderNumber, dataset)  

            'Create a report data source for the sales   
            'order detail data  
            Dim dsSalesOrderDetail As New ReportDataSource()  
            dsSalesOrderDetail.Name = "SalesOrderDetail"  
            dsSalesOrderDetail.Value = _  
                dataset.Tables("SalesOrderDetail")  

            localReport.DataSources.Add(dsSalesOrderDetail)  

            'Create a report parameter for the sales order number   
            Dim rpSalesOrderNumber As New ReportParameter()  
            rpSalesOrderNumber.Name = "SalesOrderNumber"  
            rpSalesOrderNumber.Values.Add("SO43661")  

            'Set the report parameters for the report  
            Dim parameters() As ReportParameter = {rpSalesOrderNumber}  
            localReport.SetParameters(parameters)  

        End If  

    End Sub  

    Private Sub GetSalesOrderData(ByVal salesOrderNumber As String, _  
                               ByRef dsSalesOrder As DataSet)  

        Dim sqlSalesOrder As String = _  
            "SELECT SOH.SalesOrderNumber, S.Name AS Store, " & _  
            "       SOH.OrderDate, C.FirstName AS SalesFirstName, " & _  
            "       C.LastName AS SalesLastName, E.Title AS " & _  
            "       SalesTitle, SOH.PurchaseOrderNumber, " & _  
            "       SM.Name AS ShipMethod, BA.AddressLine1 " & _  
            "       AS BillAddress1, BA.AddressLine2 AS " & _  
            "       BillAddress2, BA.City AS BillCity, " & _  
            "       BA.PostalCode AS BillPostalCode, BSP.Name " & _  
            "       AS BillStateProvince, BCR.Name AS " & _  
            "       BillCountryRegion, SA.AddressLine1 AS " & _  
            "       ShipAddress1, SA.AddressLine2 AS " & _  
            "       ShipAddress2, SA.City AS ShipCity, " & _  
            "       SA.PostalCode AS ShipPostalCode, SSP.Name " & _  
            "       AS ShipStateProvince, SCR.Name AS " & _  
            "       ShipCountryRegion, CC.Phone AS CustPhone, " & _  
            "       CC.FirstName AS CustFirstName, CC.LastName " & _  
            "       AS CustLastName " & _  
            "FROM   Person.Address SA INNER JOIN " & _  
            "       Person.StateProvince SSP ON " & _  
            "       SA.StateProvinceID = SSP.StateProvinceID " & _  
            "       INNER JOIN Person.CountryRegion SCR ON " & _  
            "       SSP.CountryRegionCode = SCR.CountryRegionCode " & _  
            "       RIGHT OUTER JOIN Sales.SalesOrderHeader SOH " & _  
            "       LEFT OUTER JOIN  Person.Contact CC ON " & _  
            "       SOH.ContactID = CC.ContactID LEFT OUTER JOIN" & _  
            "       Person.Address BA INNER JOIN " & _  
            "       Person.StateProvince BSP ON " & _  
            "       BA.StateProvinceID = BSP.StateProvinceID " & _  
            "       INNER JOIN Person.CountryRegion BCR ON " & _  
            "       BSP.CountryRegionCode = " & _  
            "       BCR.CountryRegionCode ON SOH.BillToAddressID " & _  
            "       = BA.AddressID ON  SA.AddressID = " & _  
            "       SOH.ShipToAddressID LEFT OUTER JOIN " & _  
            "       Person.Contact C RIGHT OUTER JOIN " & _  
            "       HumanResources.Employee E ON C.ContactID = " & _  
            "       E.ContactID ON SOH.SalesPersonID = " & _  
            "       E.EmployeeID LEFT OUTER JOIN " & _  
            "       Purchasing.ShipMethod SM ON SOH.ShipMethodID " & _  
            "       = SM.ShipMethodID LEFT OUTER JOIN Sales.Store" & _  
            "        S ON SOH.CustomerID = S.CustomerID " & _  
            "WHERE  (SOH.SalesOrderNumber = @SalesOrderNumber)"  

        Using connection As New SqlConnection( _  
                      "Data Source=(local); " & _  
                      "Initial Catalog=AdventureWorks; " & _  
                      "Integrated Security=SSPI")  

            Dim command As New SqlCommand(sqlSalesOrder, connection)  

            Dim parameter As New SqlParameter("SalesOrderNumber", _  
                salesOrderNumber)  
            command.Parameters.Add(parameter)  

            Dim salesOrderAdapter As New SqlDataAdapter(command)  

            salesOrderAdapter.Fill(dsSalesOrder, "SalesOrder")  

        End Using  

    End Sub  

    Private Sub GetSalesOrderDetailData( _  
                           ByVal salesOrderNumber As String, _  
                           ByRef dsSalesOrder As DataSet)  

        Dim sqlSalesOrderDetail As String = _  
            "SELECT  SOD.SalesOrderDetailID, SOD.OrderQty, " & _  
            "        SOD.UnitPrice, CASE WHEN " & _  
            "        SOD.UnitPriceDiscount IS NULL THEN 0 " & _  
            "        ELSE SOD.UnitPriceDiscount END AS " & _  
            "        UnitPriceDiscount, SOD.LineTotal, " & _  
            "        SOD.CarrierTrackingNumber, " & _  
            "        SOD.SalesOrderID, P.Name, P.ProductNumber " & _  
            "FROM    Sales.SalesOrderDetail SOD INNER JOIN " & _  
            "        Production.Product P ON SOD.ProductID = " & _  
            "        P.ProductID INNER JOIN " & _  
            "        Sales.SalesOrderHeader SOH ON " & _  
            "        SOD.SalesOrderID = SOH.SalesOrderID " & _  
            "WHERE   (SOH.SalesOrderNumber = @SalesOrderNumber) " & _  
            "ORDER BY SOD.SalesOrderDetailID"  

        Using connection As New SqlConnection( _  
                      "Data Source=(local); " & _  
                      "Initial Catalog=AdventureWorks; " & _  
                      "Integrated Security=SSPI")  

            Dim command As New SqlCommand(sqlSalesOrderDetail, _  
                                          connection)  

            Dim parameter As New SqlParameter("SalesOrderNumber", _  
                salesOrderNumber)  
            command.Parameters.Add(parameter)  

            Dim salesOrderDetailAdapter As New SqlDataAdapter(command)  

            salesOrderDetailAdapter.Fill(dsSalesOrder, _  
                "SalesOrderDetail")  

        End Using  

    End Sub  

End Class  

Vedere ancheSee Also

Integrazione di Reporting Services utilizzando i controlli ReportViewerIntegrating Reporting Services Using the ReportViewer Controls
Raccolta dei dati 2016 controllo ReportViewerData collection in ReportViewer Control 2016