Reporting Services報表,使用ASP.NET實作查詢介面
介面實作(xxx.aspx):
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ajaxManager" runat="server" />
<div>
<fieldset>
<legend>查詢條件</legend>
<table cellspacing="1" width="">
<tbody>
<tr>
<%-- 日期區間--%>
<td>
<asp:Label ID="lblDateS" runat="server" AssociatedControlID="txtDateS" Text="日期區間" />
</td>
<td>
<asp:TextBox ID="txtDateS" runat="server" />
<asp:CalendarExtender ID="calcExS" runat="server" TargetControlID="txtDateS" Format="yyyy/MM/dd"
PopupButtonID="img1" />
<asp:TextBoxWatermarkExtender ID="txtWaterMarkS" runat="server" TargetControlID="txtDateS"
WatermarkText="請輸入起始日" />
<asp:Image ID="img1" runat="server" ImageUrl="~\images\Calendar_scheduleHS.png" />
<asp:RangeValidator ID="dateSRange" runat="server" ControlToValidate="txtDateS" ErrorMessage="無效的日期"
Display="Dynamic" />
</td>
<td>
~
</td>
<td>
<asp:TextBox ID="txtDateE" runat="server" />
<asp:CalendarExtender ID="calcExE" runat="server" TargetControlID="txtDateE" Format="yyyy/MM/dd"
PopupButtonID="img2" />
<asp:TextBoxWatermarkExtender ID="txtWaterMarkE" runat="server" TargetControlID="txtDateE"
WatermarkText="請輸入結束日" />
<asp:Image ID="img2" runat="server" ImageUrl="~\images\Calendar_scheduleHS.png" />
<asp:RangeValidator ID="dateERange" runat="server" ControlToValidate="txtDateE" ErrorMessage="無效的日期"
Display="Dynamic" />
<asp:CompareValidator ID="cmpDate" runat="server" Display="Dynamic" ErrorMessage="日期必須大於起始日"
ControlToCompare="txtDateS" ControlToValidate="txtDateE" Type="Date" Operator="GreaterThan" />
</td>
</tr>
<tr>
<%--商品種類 --%>
<td>
<asp:Label ID="lblItemClass" runat="server" Text="商品種類" AssociatedControlID="cmbItemType" />
</td>
<td colspan="3">
<asp:ComboBox ID="cmbItemType" runat="server" />
</td>
</tr>
<tr>
<%--取前幾名 --%>
<td>
<asp:Label ID="lblTopDown" runat="server" AssociatedControlID="txtTopDown" Text="取前" />
</td>
<td>
<asp:TextBox ID="txtTopDown" runat="server" Columns="5" Text="20" />
<asp:RangeValidator ID="topDownRange" runat="server" ControlToValidate="txtTopDown"
Display="Dynamic" Type="Integer" MaximumValue="100" MinimumValue="1" ErrorMessage="請輸入前N大排名" />
<asp:Label ID="lblTopDown2" runat="server" Text="大" />
</td>
<td align="right">
<asp:Button ID="btnView" runat="server" Text="預覽" />
</td>
</tr>
</tbody>
</table>
</fieldset>
<%--嵌入報表元件--%>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="新細明體" Font-Size="10pt"
Height="600px" Width="100%" ProcessingMode="Remote">
</rsweb:ReportViewer>
</div>
</form>
</body>
<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="ReportSupplyDownCount.aspx.cs" Inherits="ReportView._Default" %>
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %> <!--Ajax元件-->
<%@ 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">
程式碼實作(xxx.cs):
public partial class _Default : System.Web.UI.Page
{
string reportServer;
protected void Page_Load(object sender, EventArgs e)
{
reportServer = WebConfigurationManager.AppSettings["ReportServer"]; //WebConfigurationManager --> using System.Web.Configuration;
dateSRange.Type = ValidationDataType.Date;
dateSRange.MaximumValue = DateTime.Today.ToString("yyyy/MM/dd");
dateSRange.MinimumValue = Convert.ToDateTime("2000/1/1").ToString("yyyy/MM/dd");
txtDateS.Text = DateTime.Now.AddDays(-30).ToString("yyyy/MM/dd");
dateERange.Type = ValidationDataType.Date;
dateERange.MaximumValue = DateTime.Today.ToString("yyyy/MM/dd");
dateERange.MinimumValue = Convert.ToDateTime("2000/1/1").ToString("yyyy/MM/dd");
txtDateE.Text = DateTime.Today.ToString("yyyy/MM/dd");
ArrayList itemClassList = new ArrayList(); //ArrayList --> using System.Web.Configuration;
itemClassList.Add("不區分");
itemClassList.Add("題庫");
itemClassList.Add("試卷");
itemClassList.Add("模擬考");
cmbItemType.DataSource = itemClassList;
cmbItemType.DataBind();
ReportViewer1.ServerReport.ReportServerUrl = new Uri(reportServer);
ReportViewer1.ServerReport.ReportPath = @"/Report Parts/QrySupplyDownCount";
//關閉報表預設查詢條件,這邊要用ASP.NET來自訂畫面
ReportViewer1.ShowParameterPrompts = false;
ReportViewer1.ShowToolBar = true;
btnView.Click += new EventHandler(btnView_Click);
}
void btnView_Click(object sender, EventArgs e)
{
List<ReportParameter> paramList = new List<ReportParameter>(); //ReportParameter --> using Microsoft.Reporting.WebForms;
paramList.Add(new ReportParameter("topDown", txtTopDown.Text, false)); //與畫面上的控件項進行綁定
paramList.Add(new ReportParameter("qry_Type", cmbItemType.Text, false));
paramList.Add(new ReportParameter("qry_DateS", txtDateS.Text, false));
paramList.Add(new ReportParameter("qry_DateE", txtDateE.Text, false));
ReportViewer1.ServerReport.SetParameters(paramList);
ReportViewer1.ServerReport.Refresh();
}
}
補充說明:
※將報表"處理模式"設為 ReportViewer1.ProcessingMode = ProcessingMode.Remote;
資料的驗證可以使用RangeValidator 、CompareValidator 。