Simplify Asp.net report with reflection

Source: Internet
Author: User

There are several reports and the query conditions are the same. Only the difference between several columns in the gridview is that the previous graph is fast and the original report is displayed. copy the aspx file and change it to report1.aspx. Then, modify the column in it, and then check the data in the background database and fill it in the report.

Today, I am finally impatient. This method is indeed too stupid. The solution is as follows:

1. Create an ireport interface. All reports implement this interface.

Public interface ireport
{
// A set of all available columns in the report (not all columns are displayed for each query)
Dictionary <string, string> dictcolmapping {Get ;}

 

// (This query) The keyfield and headertext columns displayed in the report

Dictionary <string, int> dictcolindexmapping {Get ;}

/// (This query) The sortexpressfield column displayed in the report and the index set of the position

List <reportcolumn> reportcolumncollection {Get ;}

// Export the Excel file name
String toexcelname {Get ;}

 

// Unique ID of the Report for security verification
String reportresourceid {Get ;}

 

// Generate a report. The input condition is an array of parameters.
Dataset proccessreport (Params object [] paramlist );
}

 

2. Create a reportcolumn class.

public class reportcolumn
{< br> Public String columndatafield {set; get ;}< br> Public String columnheadertext {set; get ;}
Public String columnsortexpression {set; get ;}< br> Public int columnindex {set; get ;}

Public reportcolumn (string _ columndatafield, string _ columnheadertext, string _ columnsortexpression, int _ columnindex)
{
This. columndatafield = _ columndatafield;
This. columnheadertext = _ columnheadertext;
This. columnsortexpression = _ columnsortexpression;
This. columnindex = _ columnindex;
}
}

 

3. Create an abstract base class for a report, which contains some common methods for processing similar reports.

Public abstract class clsreportbase
{}

4. Create an actual report class

public class clsreport_salesorder: clsreportbase, ireport
{< br> private list _ reportcolumncollection; // a set of all available columns for the report (not all columns are displayed for each query)
private dictionary _ dictcolmapping; // (this query) the keyfield and headertext columns displayed in the Report
private dictionary _ dictcolindexmapping; // (this query) sortexpressfield and index set of the position displayed in the Report

Public clsreport_salesorder ()
{< br> _ reportcolumncollection = new list ();
_ dictcolmapping = new dictionary ();
_ dictcolindexmapping = new dictionary ();
fillreportcolumncollection ();
}

/// <Summary>
/// Fill in all available column sets of the report
/// </Summary>
Private void fillreportcolumncollection ()
{
Addcolumntocollection (New reportcolumn ("Province", 0 ));
Addcolumntocollection (New reportcolumn ("vertical", "industry", "industry", 1 ));
Addcolumntocollection (New reportcolumn ("productline", "product", "product", 2 ));

Addcolumntocollection (New reportcolumn ("subtotal", "order amount ($)", "order amount ($)", 3 ));
}

///


// a set of all available columns in the report. (Not all columns are displayed for each query)
//
public list reportcolumncollection
{< br> Get
{< br> return _ reportcolumncollection;
}< BR >}

///


/// (this query) the keyfield and headertext columns displayed in the Report
///
Public dictionary dictcolmapping
{< br> Get
{< br> return _ dictcolmapping;
}< BR >}

///


/// (this query) the sortexpressfield column displayed in the report and the index set of the location
///
Public dictionary dictcolindexmapping
{< br> Get
{< br> return _ dictcolindexmapping;
}< BR >}

///


/// (this query) this report needs to be formatted as a column set of currency
///
public list moneyformatcolumncollection
{< br> Get
{
return _ moneyformatcolumncollection;
}< BR >}

//


// report name
///
Public String toexcelname
{
get
{< br> return "Order Statistics .xls ";
}< BR >}

//


// unique report Code
///
Public String reportresourceid
{
Get
{
return "12345 ";
}< BR >}

private void addcolumntocollection (reportcolumn column)
{< br> _ reportcolumncollection. add (column);
_ dictcolmapping. add (column. columndatafield, column. columnheadertext);
_ dictcolindexmapping. add (column. columnsortexpression, column. columnindex);
}

/// <Summary>
/// Generate a report. The input condition is an array of parameters.
/// </Summary>
/// <Param name = "paramlist"> </param>
/// <Returns> </returns>
Public dataset proccessreport (Params object [] paramlist)
{

... Parse the query conditions from the paramlist parameter list, and then retrieve data from the database.
}

}

5. The gridview in the front-end ASPX page

<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" cellpadding = "2" cssclass = "autotable" allowsorting = "false">
<Columns>
</Columns>
</ASP: gridview>

 

6. On the front-end Aspx. CS page, the Report class name sent from the URL reflects the report instance.

 

Protected void page_load (Object sender, eventargs E)
{
_ Initreport (this. Request. querystring ["report"]);

}

Private void _ initreport (string querystring)
{
String reportclassname = querystring;
String reportassemblyname = "test ";
String reportfullclassname = reportassemblyname + "." + reportclassname;
Report = (ireport) Assembly. Load (reportassemblyname). createinstance (reportfullclassname );

_ Dictcolmapping = new dictionary <string, string> ();
_ Dictcolmapping = report. dictcolmapping;

_ Dictcolindexmapping = new dictionary <string, int> ();
_ Dictcolindexmapping = report. dictcolindexmapping;

Gridview1.columns. Clear ();
Foreach (reportcolumn column in report. reportcolumncollection)
{
Boundfield BF = new boundfield ();
BF. datafield = column. columndatafield;
BF. headertext = column. columnheadertext;
BF. sortexpression = column. columnsortexpression;
BF. headerstyle. Wrap = false;
BF. itemstyle. Wrap = false;
Gridview1.columns. Add (BF );
}
}

 

Protected void btn_search_click (Object sender, eventargs E)
{
Object [] arr = new object [] {userobject, conditionobject ...};
Dataset m_data = report. proccessreport (ARR );

Hidesomecolumns (m_data); // if needed, you can block columns not displayed here and set the column visiable to false.

This. gridview_salesordersummarization.datasource = m_data.tables [0];
This. gridview_salesordersummarization.databind ();
}

 
7. In this way, only one aspx file is used to generate multiple reports. You only need to pass the type as a parameter.

Report. aspx? Report = clsreport_salesorder
Report. aspx? Report = clsreport_salesorder2

 

This simplifies the front-end aspxProgramNumber to facilitate system maintenance.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.