1. 程式人生 > >【Stimulsoft Reports.Net教程】在報表中使用變數

【Stimulsoft Reports.Net教程】在報表中使用變數

下載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;
}

示例程式碼的結果如下圖所示:

Stimulsoft

檢視原文,下載示例