How to Implement the Repeater control to dynamically change the information of columns (Header, Item, and Foot)

Source: Internet
Author: User

You need to develop a small report that displays the number of summary statistics for the last five months. The report changes with the change of the month, for example. The first column [Department] is fixed, and the second to sixth columns, that is to say, the Nov 2012 column will disappear next month, the subsequent column will move forward, and the last column Mar 2013 will change to Apr 2013.

The bottom row shows the total number of each column (except the first column ).


For this report, Insus. NET decided to use the Repeater control. The difficulty lies in the dynamic display of the column name from the second column to the sixth column, and the binding of data. If the last row calculates the total, as long as the above dynamic binding is completed, it is not a problem. The FooterTemplate of the Repeater shows the total method of a column. For details, refer to this article: Limit.

The first is HeaderTemplate, which is embedded with a table. six columns are designed, each column pulls a Label, and the Label IDs are set separately. It is best to make them look like the ordinal number with an index:

The next step is the ItemTemplate design. You can refer to the HeaderTemplate design method:

Finally, the FooterTemplate design, design the Label ID, td text alignment and background color:


The labels of the three templates have different IDs, but there are some rules, so that FindControl can be conveniently configured in the background and control can be reconstructed in the future.

The above are all html designs, and the following will be program development.
Copy codeThe Code is as follows:
// Instantiate the object and declare a DataTable object objDt.
PrintLog objPrintLog = new PrintLog ();
DataTable objDt;

// Declare five variables, which will be used to store the number of each part in the five months
Decimal c1, c2, c3, c4, c5;

Bind the data obtained from the database to the Repeater control: 
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Data_Binding ();
}
}

Private void Data_Binding ()
{
(IUserControlContainer) this. Master). ShowControl = true;

CultureInfo oCultureInfo = new CultureInfo ("en"); // set the regional culture
DateTime currentDateTime = DateTime. Now; // get the current date and time
ObjPrintLog. Year = currentDateTime. Year; // gets the Year value.
ObjPrintLog. Month = currentDateTime. Month; // obtain the Month value assignment.
ObjPrintLog. Months = 5; // The last five consecutive Months

ObjDt = objPrintLog. GetSummaryReportByYearMonth ();
This. RepeaterLFMS. DataSource = objDt;
This. RepeaterLFMS. DataBind ();
}

When the phone rings, you need to answer the phone at about 1 minute 36 seconds. By the way, go to Coffee.

The OnItemDataBound event of the Repeater control will be shown below. Insus. NET will also describe the header, Item, and Foot in a graphic column:


Item binding:

Finally, the Foot code is as follows:


Haha, it's done. It is believed that when it is running, it will move forward with time (the month changes), its corresponding column name, data will also change.

Related Article

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.