在本节中,将编写代码以使用子目录中包含的非嵌入式报表的列表来填充 DropDownList 控件。
注意 在本教程中,将使用网站的子目录中包含的报表来填充 DropDownList 控件。也可以处理文件系统中其他位置的文件夹。如果已找到包含一些报表的文件夹,则可以跳过下一过程,并直接转到“填充 DropDownList 控件”。
创建 Reports 文件夹
在“解决方案资源管理器”中,右击用粗体字显示的项目名,指向“添加文件夹”,然后单击“常规文件夹”。
将该文件夹命名为“Reports”。
在“解决方案资源管理器”中,右击“Reports”文件夹,然后单击“添加现有项”。
在“添加现有项”对话框中,将“文件类型”设置为“所有文件 (*.*)”。
浏览到 Crystal Reports 示例报表目录的“功能示例”文件夹中的“Hierarchical Grouping.rpt”文件。(有关示例报表的位置,请参见示例报表目录。)
**注意 **这个 Hierarchical Grouping 报表将从 Access 数据库 xtreme.mdb 中检索数据。如果尚未验证此数据库的位置及其 ODBC DSN 配置,请参见需要验证的事项。
单击选中“Hierarchical Grouping.rpt”文件,然后单击“打开”。
Hierarchical Grouping.rpt 文件随即添加到项目中。
在转到下一过程之前,从示例报表目录中添加两个其他报表。
有关添加 Reports 文件夹的部分即告完成。
填充 DropDownList 控件
在此过程中,将填充 Reports 子目录中包含的报表的排序列表。
打开 Web 窗体。
从“视图”菜单中,单击“代码”。
在类签名上,在类顶部为 System.IO 和 System.Collections 命名空间添加
"Imports"[Visual Basic]或"using"[C#]声明。[Visual Basic]
Imports System.IO Imports System.Collections
[C#]
using System.IO; using System.Collections;
在类级上,创建新的
ReportDocument变量reportDocument。[Visual Basic]
Private myReportDocument As ReportDocument
[C#]
private ReportDocument reportDocument;
在
page_init()方法中,创建一个Not IsPostBack条件块。[Visual Basic]
If Not IsPostBack Then Else End If
[C#]
if(!IsPostBack) { } else { }**注意 **Not IsPostBack 条件块用于封装只应在首次加载页面时才运行的代码。在 Not IsPostBack 条件块中,控件通常都绑定到数据值,这样它们的数据值(以及所有后续控件事件)才不会在重新加载页面时重置。
在
Not IsPostBack条件块中,创建一个保存 Reports 文件夹路径的字符串变量。[Visual Basic]
Dim myReportPath As String = Server.MapPath("Reports/")[C#]
string reportPath = Server.MapPath("Reports/");**注意 **如果跳过上一过程并且包含一个现有报表目录,请输入该目录的文件路径以替换 "Reports/"。
使用
Directory.GetFiles()方法来检索 Reports 文件夹的内容。将结果分配给reports数组。[Visual Basic]
Dim reports() As String = Directory.GetFiles(myReportPath, "*.rpt")
[C#]
string[] reports = Directory.GetFiles(reportPath, "*.rpt");
在下一行,实例化
SortedList类并将它传递给其父界面IDictionary。[Visual Basic]
Dim mySortedList As IDictionary = New SortedList
[C#]
IDictionary sortedList = new SortedList();
下一步,创建
For Each循环,此循环在reports的每个元素间循环。[Visual Basic]
For Each path As String In reports Next
[C#]
foreach (string path in reports) { }下三行代码使用字符串处理方法,从每个报表的名称中删除文件路径。将这些行插入到
For Each循环内。[Visual Basic]
Dim reportNamePrefix As Integer = path.LastIndexOf("") + 1 Dim reportNameLength As Integer = path.Length - reportNamePrefix Dim reportName As String = path.Substring(reportNamePrefix, reportNameLength)[C#]
int reportNamePrefix = path.LastIndexOf(@"") + 1; int reportNameLength = path.Length - reportNamePrefix; string reportName = path.Substring(reportNamePrefix, reportNameLength);
仍然在
For Each循环中添加一行代码,该行代码将截断的报表名称添加到sortedListIDictionary。[Visual Basic]
mySortedList.Add(path, reportName)
[C#]
sortedList.Add(path, reportName);
将已排序的列表绑定到 DropDownList 控件
在本节中,您将了解如何将 IDictionary 数据源绑定到 DropDownList 控件。首先,定义 IDictionary 集合的元素与 DropDownList 字段之间的关系。然后,将 IDictionary 数据源绑定到 DropDownList 控件。
?
foreach????,??????DropDownList? Value ?????sortedList??? Key ???[Visual Basic]
reportsList.DataTextField = "value"
[C#]
reportsList.DataTextField = "value";
???,?
DropDownList??? Value ?????sortedList??? Key ???[Visual Basic]
reportsList.DataValueField = "key"
[C#]
reportsList.DataValueField = "key";
?
SortedList?????DropDownList???dataSource???[Visual Basic]
reportsList.DataSource = mysortedList
[C#]
reportsList.DataSource = sortedList;
最后,将数据源绑定到
DropDownList。[Visual Basic]
reportsList.DataBind()
[C#]
reportsList.DataBind();
现在您完成了
If Not IsPostBack条件块的内容。在下一节中,将在Else条件块内编写代码以定义刷新页面而未给ReportDocument实例分配新值时所发生的情况。在 else 语句内,将 ReportDocument 实例分配给会话中当前保存的报表。
[Visual Basic]
myReportDocument = CType(Session("myReportDocument"), ReportDocument)[C#]
reportDocument = (ReportDocument)Session["reportDocument"];
**注意 **由于 Session 只返回一般对象,您必须将报表转换为报表类型。无论您使用嵌入式还是非嵌入式报表,都要将检索到的对象转换为 ReportDocument 类型。
在下一行上,将 ReportDocument 实例绑定到 CrystalReportViewer 控件。
[Visual Basic]
myCrystalReportViewer.ReportSource = myReportDocument
[C#]
crystalReportViewer.ReportSource = reportDocument;
从“文件”菜单中选择“全部保存”。
从“生成”菜单中选择“生成解决方案”。
如果生成过程中出错,请立即纠正。
在下一节中,将添加一个 click 事件,根据 reportsList DropDownList 控件中选择的内容来绑定报表。
继续到添加 Click 事件以绑定报表.
请参见
© 2005 Business Objects SA. All Rights Reserved
| Business Objects http://www.china.businessobjects.com/ や穿狝叭 http://www.china.businessobjects.com/BOindex/support/ |