Repeater Control Dynamic Change column (Header,item and foot) information (refactoring CS) _ Practical Tips

Source: Internet
Author: User

There's a morning share. Repeater control Dynamic Change column (Header,item and foot) information is dynamically changing the contents of some columns.

Although it is not completely dynamic, it has reached the initial desired effect. If you are careful, you can easily get it out. Now open another, insus.net is not another write, but want to refactor the CS code, because the previous piece of code, although simple, but too much code redundancy.

Refactoring start:
First, delete these five variables, because the five variables are not needed in the refactoring process.

Copy Code code as follows:

Declares 5 variables that will be used to store the number of each part of the 5 month
Decimal C1, C2, C3, C4, C5;

The next thing to change is declaring a constant, which is used in many places:
Copy Code code as follows:

const int dynamiccolumns = 5;

Put the following sentence in the program
Copy Code code as follows:

Objprintlog.months = 5; 5 months in a row recently

To
Copy Code code as follows:

Objprintlog.months = Dynamiccolumns;

In other words, use constant variables to replace the old code "5".

Next, we refactor the code for the header of the Repwater control, and, for the sake of comparison, insus.net the previous corresponding picture here:

Refactor as follows:

Copy Code code 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 a contrast, you can see the code in the variable code. The following is the Repwater Control Item section:

The code after refactoring the old code, the 16th line of code, is to judge the first column, because it is a string, and therefore separately excluded. Line 23rd, use ViewState to replace the 5 variables of the old program.

Copy Code code 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;
}
}
}
}

And finally, the foot refactoring:

Foot refactoring good code, line 14th is to determine whether the first column, line 17th, is to assign the value of ViewState to the label.

Copy Code code 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 is a code that reduces redundancy in the event that the program's functional requirements are unchanged.

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.