Selecting report and datasource for reportviewer from dropdown

When this code inside my content page loads I can select any of the reports and it shows up in the reportviewer. However, if I go and select a different report from the dropdown and click the button it reloads the report I selected after the very first page load. The only way to get a different report to display is to do a ctrl refresh.

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<br />

<asp:DropDownList ID="DropDownList1" runat="server">

<asp:ListItem Value="1">All Projects with Date and Manager</asp:ListItem>

<asp:ListItem Value="2">All Active Projects with Date and Manager</asp:ListItem>

<asp:ListItem Value="3">All Projects By Type</asp:ListItem>

<asp:ListItem Value="4">Row Report</asp:ListItem>

<asp:ListItem Value="5">Let Date and Personnel</asp:ListItem>

<asp:ListItem Value="6">Let Date</asp:ListItem>

<asp:ListItem Value="7">Water</asp:ListItem>

</asp:DropDownList>&nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"

Text="Button" /><br />

<br />

<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt"

Height="400px" Width="750px" DocumentMapWidth="50%" ZoomPercent="75">

<LocalReport>

</LocalReport>

</rsweb:ReportViewer>

&nbsp;<asp:ObjectDataSource ID="MainQueryObjectDataSource" runat="server"

OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="ProjectsDBTableAdapters.ProjectsTableAdapter">

</asp:ObjectDataSource>

<br />

<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"

OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataWithActive" TypeName="ProjectsDBTableAdapters.ProjectsTableAdapter">

</asp:ObjectDataSource>

<br />

<asp:ObjectDataSource ID="MajorWaterObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}"

SelectMethod="GetMajorWater" TypeName="ProjectsDBTableAdapters.ProjectsTableAdapter">

</asp:ObjectDataSource>

<br />

<asp:ObjectDataSource ID="ROWTitleStatusObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}"

SelectMethod="GetDataROWTitleStatus" TypeName="ProjectsDBTableAdapters.ProjectsTableAdapter">

</asp:ObjectDataSource>

if (DropDownList1.SelectedValue == "1")

{

ReportViewer1.LocalReport.ReportPath = "DesignProjects\\Reports\\AllProjectsDateManger.rdlc";

ReportViewer1.LocalReport.DataSources.Clear();

Microsoft.Reporting.WebForms.ReportDataSource DataSource = new Microsoft.Reporting.WebForms.ReportDataSource();

DataSource.DataSourceId = "MainQueryObjectDataSource";

DataSource.Name = "ProjectsDB_Projects";

ReportViewer1.LocalReport.DataSources.Add(DataSource);

ReportViewer1.DataBind();

}

else if (DropDownList1.SelectedValue == "2")

{

ReportViewer1.LocalReport.ReportPath = "DesignProjects\\Reports\\AllProjectsDateManger.rdlc";

ReportViewer1.LocalReport.DataSources.Clear();

Microsoft.Reporting.WebForms.ReportDataSource DataSource = new Microsoft.Reporting.WebForms.ReportDataSource();

DataSource.DataSourceId = "ObjectDataSource2";

DataSource.Name = "ProjectsDB_Projects";

ReportViewer1.LocalReport.DataSources.Add(DataSource);

ReportViewer1.DataBind();

}

There are like 5 more of these



Answer this question

Selecting report and datasource for reportviewer from dropdown

  • Radu B

    Combining the two code samples above, I was able to get this working with the following (before setting the ReportPath):

    ControlCollection coll = ReportViewer1.Parent.Controls;
    int oldIndex = coll.IndexOf(ReportViewer1);
    coll.Remove(ReportViewer1);
    ReportViewer1 = New ReportViewer;
    ReportViewer1.Height = Unit.Parse("9.5in");
    ReportViewer1.Width = Unit.Parse("7in");
    coll.AddAt(oldIndex, ReportViewer1);


  • Duncan Stewart

    This is a known limitation with the report viewer in local mode. With the winforms reportviewer, you can call ReportViewer.Reset() to create a new local report. But this method is missing in webforms (we hope to add it in the next update). Until then, you can create a new report viewer on the form to accomplish this:

    ControlCollection coll = oldViewer.Parent.Controls;
    int oldIndex = coll.IndexOf(oldViewer);
    ReportViewer newViewer = new ReportViewer();
    coll.AddAt(oldIndex, newViewer);
    coll.Remove(oldViewer);


  • Selecting report and datasource for reportviewer from dropdown