【Stimulsoft Reports.Net教程】在报表中使用变量
此示例显示Web报表中的使用变量(报表参数)。在开始页面上,表单中有几个参数 - 例如,这是一个简单的用户配置文件。填充后,这些参数将传递到Report.aspx页面。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Using_Variables_in_the_Report.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Using Variables in the Report</title> </head> <body> <h2><span style="color: #0066ff">Variables Example</span></h2> <h3>This sample demonstrates how to operate with report variables</h3> <form id="form1" action="Report.aspx" > <div> <table style="width:100%;"> <tr> <td style="width:100px;"><label for="name">Name:</label></td> <td><input id="Name" type="text" name="name" /></td> </tr> <tr> <td><label for="surname">Surname:</label></td> <td><input id="Surname" type="text" name="surname" /></td> </tr> <tr> <td><label for="email">E-Mail:</label></td> <td><input id="EMail" type="text" name="email" /></td> </tr> <tr> <td><label for="address">Address:</label></td> <td><input id="Address" type="text" name="address" /></td> </tr> <tr> <td><label for="sex">Sex:</label></td> <td> <input id="Radio1" type="radio" name="sex" value="true" checked="checked" />Male <input id="Radio2" type="radio" name="sex" value="false" />Female </td> </tr> </table> <p> <input id="Submit" type="submit" value="Submit" /> </p> </div> </form> <br /> <a href="Design.aspx">Design Report</a> </body> </html>
在Page_Load事件的Report.aspx页面上,我们将参数值分配给报表。参数名称是报表模板的字典中变量的名称。
protected void Page_Load(object sender, EventArgs e) { StiReport report = new StiReport(); DataSet data = new DataSet(); report.Load(Server.MapPath(@"Reports\Variables.mrt")); report.Compile(); data.ReadXml(Server.MapPath(@"Data\Demo.xml")); if (Request.QueryString["name"] != null) report["Name"] = Request.QueryString["name"]; else report["Name"] = ""; if (Request.QueryString["surname"] != null) report["Surname"] = Request.QueryString["surname"]; else report["Surname"] = ""; if (Request.QueryString["email"] != null) report["Email"] = Request.QueryString["email"]; else report["Email"] = ""; if (Request.QueryString["address"] != null) report["Address"] = Request.QueryString["address"]; else report["Address"] = ""; if (Request.QueryString["sex"] != null) { bool isMale = true; Boolean.TryParse(Request.QueryString["sex"], out isMale); report["Sex"] = isMale; } else { report["Sex"] = false; } report.RegData(data); StiWebViewer1.Report = report; }
示例代码的结果如下图所示: