Repeater control Dynamic Change of column (Header, Item, and Foot) Information (rebuilding cs)

Source: Internet
Author: User

In the morning, we shared "Repeater control dynamic change columns (Header, Item, and Foot) information", which is used to dynamically change some columns.

Although it is not completely dynamic, it has achieved the initial effect. If you are careful, you can easily get it out. Now I have another article. Insus. NET does not re-write but wants to refactor the cs code, because the code in the previous article is simple but redundant.

Reconstruction starts:
Delete these five variables first, because they are no longer needed during the refactoring process.
Copy codeThe Code is as follows:
// Declare five variables, which will be used to store the number of each part in the five months
Decimal c1, c2, c3, c4, c5;

Next, we need to change it to declare a constant, which is used in many places:
Copy codeThe Code is as follows:
Const int dynamicColumns = 5;

Put the following sentence in the program
Copy codeThe Code is as follows:
ObjPrintLog. Months = 5; // The last five consecutive Months

Changed:
Copy codeThe Code is as follows:
ObjPrintLog. Months = dynamicColumns;

That is to say, replace the "5" of the old Code with constant variables ".

Next, we reconstruct the Header code of the Repwater control. For better comparison, Insus. NET references the image corresponding to the previous article here:

 

The reconstruction is as follows:
Copy codeThe Code is as follows:
Protected void RepeaterLFMS_ItemDataBound (object sender, RepeaterItemEventArgs e)
{
If (e. Item. ItemType = ListItemType. Header)
{
If (e. Item. FindControl ("LabelH0 ")! = Null
& E. Item. FindControl ("LabelH1 ")! = Null
& E. Item. FindControl ("LabelH2 ")! = Null
& E. Item. FindControl ("LabelH3 ")! = Null
& E. Item. FindControl ("LabelH4 ")! = Null
& E. Item. FindControl ("LabelH5 ")! = Null)
{
For (int I = 0; I <= dynamicColumns; I ++)
{
Label lh = (Label) e. Item. FindControl ("LabelH" + I. ToString ());
Lh. Text = objDt. Columns [I]. ColumnName;
}
}
}

As long as there is a one-to-one comparison, you can see the code in the changed code. The following is the Item section of the Repwater control:

The code after the reconstruction of the old Code, 16th lines of code, is to judge the first column, because it is a string, so it is excluded separately. In row 3, ViewState is used to replace the five variables of the old program.
Copy codeThe Code is as follows:
If (e. Item. ItemType = ListItemType. AlternatingItem | e. Item. ItemType = ListItemType. Item)
{
DataRowView drv = (DataRowView) e. Item. DataItem;
If (e. Item. FindControl ("LabelI0 ")! = Null
& E. Item. FindControl ("LabelI1 ")! = Null
& E. Item. FindControl ("LabelI2 ")! = Null
& E. Item. FindControl ("LabelI3 ")! = Null
& E. Item. FindControl ("LabelI4 ")! = Null
& E. Item. FindControl ("LabelI5 ")! = Null)
{

For (int j = 0; j <= dynamicColumns; j ++)
{
Label li = (Label) e. Item. FindControl ("LabelI" + j. ToString ());

If (j = 0)
Li. Text = drv [objDt. Columns [0]. ColumnName]. ToString ();
Else
{
Decimal v = string. IsNullOrEmpty (drv [objDt. Columns [j]. ColumnName]. ToString ())? 0: Convert. ToDecimal (drv [objDt. Columns [j]. ColumnName]. ToString ());
Li. Text = v. ToString ();

ViewState ["c" + j. ToString ()] = ViewState ["c" + j. ToString ()] = null? 0: Convert. ToDecimal (ViewState ["c" + j. ToString ()]) + v;
}
}
}
}

Finally, Foot reconstruction:

Foot restructured the code. Row 14th is to judge whether it is the first column, and row 17th is to assign the value of ViewState to the Label.
Copy codeThe Code is as follows:
If (e. Item. ItemType = ListItemType. Footer)
{
If (e. Item. FindControl ("LabelF0 ")! = Null
& E. Item. FindControl ("LabelF1 ")! = Null
& E. Item. FindControl ("LabelF2 ")! = Null
& E. Item. FindControl ("LabelF3 ")! = Null
& E. Item. FindControl ("LabelF4 ")! = Null
& E. Item. FindControl ("LabelF5 ")! = Null)
{
For (int k = 0; k <= dynamicColumns; k ++)
{
Label lf = (Label) e. Item. FindControl ("LabelF" + k. ToString ());

If (k = 0)
Lf. Text = "Total ";
Else
Lf. Text = ViewState ["c" + k. ToString ()] = null? "0": ViewState ["c" + k. ToString ()]. ToString ();
}
}
}
}

Refactoring reduces redundant code without changing the functional requirements of the program.

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.