在上一篇文章中我们讲解了在 WinForms 程序中如何给 SectionReport 添加动态排序,本文将讲解如何在 ASP.NET 程序中实现 SectionReport 的动态排序操作。

为SectionReport添加排序功能 之 WinForms

首先在 ASP.NET 程序中添加 SectionReport 报表,并连接数据源,然后我们给产品ID这一列添加动态排序操作,报表设计界面如下:

image

我们给Label2设置 HyperLink 属性为 WebForm1.aspx?sort=DESC ,并在报表的后台代码中添加以下代码,用于更改 Label2 的显示文字和 HyperLink 属性:

    public string Sort
    {
        get;
        set;
    }

    private void SectionReport1_ReportStart(object sender, EventArgs e)
    {
        if (Sort == "ASC")
        {
            this.label2.Text = "▲";
            this.label2.HyperLink = "WebForm1.aspx?sort=DESC";
        }
        else
        {
            this.label2.Text = "▼";
            this.label2.HyperLink = "WebForm1.aspx?sort=ASC";
        }            
    }

 

然后我们在 WebForm1 后台代码中获取 sort 的值,并按照设置进行排序,后台代码如下:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sort = "ASC";
            if (Request.QueryString["sort"] != null)
            {
                sort = Request.QueryString["sort"];
            }
            SectionReport1 rpt = new SectionReport1();
            rpt.Sort = sort;    
            rpt.DataSource = GetData(sort);
            WebViewer1.Report = rpt;
        }
    }

 

运行截图:

ARSort

 

源码下载:

WebApplication14.zip (43.15 kb)