Repeater控件動態(tài)變更列(Header,Item和Foot)信息(重構(gòu)cs)
早上有分享《Repeater控件動態(tài)變更列(Header,Item和Foot)信息》 ,是動態(tài)變更一些列的內(nèi)容。
雖然它算不上是完全動態(tài)化,但它已經(jīng)達到初期想要的效果。如果你稍有仔細,也能輕易把它弄出來?,F(xiàn)另開一篇,Insus.NET不是重新另外寫,而是想重構(gòu)cs的代碼,因為前一篇的代碼雖然簡單,但代碼冗余過多。
重構(gòu)開始:
首先把這五個變量刪除,因為在重構(gòu)過程中,已經(jīng)不需要這五個變量了。
//宣告5個變量,將用來存儲那5個月份每個部分的數(shù)量
decimal c1, c2, c3, c4, c5;
接下來需要改動的是宣告一個常量,很多地方使用到它:
const int dynamicColumns = 5;
把程序中的下面這句
objPrintLog.Months = 5; //最近連續(xù)5個月份
改為:
objPrintLog.Months = dynamicColumns;
也就是說,使用常量的變量去替代舊代碼的"5"。
接下來,我們重構(gòu)Repwater控件的Header的代碼,為了好對比,Insus.NET把上一篇對應(yīng)的圖片引用在這里:
重構(gòu)如下:
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;
}
}
}
只要一對比,就可以明了看到變代碼中的代碼。下面是Repwater控件Item 部分:

舊代碼重構(gòu)之后的代碼,第16行代碼,是判斷第一列,因為它是字符串,因此單獨排除。第23行,使用ViewState來替代舊程序的5個變量。
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;
}
}
}
}
最后是Foot的重構(gòu):

Foot重構(gòu)好的代碼,第14行是判斷是否為第一列,第17行,是把ViewState的值賦給Label。
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();
}
}
}
}
重構(gòu)是在程序功能要求不變的情況之下,減少冗余的代碼。
- Repeater事件OnItemCommand取得行內(nèi)控件的方法
- Repeater控件與PagedDataSource結(jié)合實現(xiàn)分頁功能
- Repeater控件實現(xiàn)編輯、更新、刪除等操作示例代碼
- Repeater怎么實現(xiàn)多行間隔顯示分隔符
- Repeater中嵌套Repeater的示例介紹
- repeater做刪除前彈窗詢問實例
- 給Repeater控件里添加序號的5種才常見方法介紹
- asp.net Repeater 數(shù)據(jù)綁定的具體實現(xiàn)(圖文詳解)
- .net JS模擬Repeater控件的實現(xiàn)代碼
- Repeater控件綁定的三種方式
- 利用js的Node遍歷找到repeater的一個字段實例介紹
- ASP.NET筆記之 Repeater的使用
- Repeater綁定dictionary數(shù)據(jù)源代碼及報錯解決
- asp.net Repeater分頁實例(PageDataSource的使用)
- asp.net中使用repeater和PageDataSource搭配實現(xiàn)分頁代碼
- Repeater里switch的使用方法
- Repeater的FooterTemplate顯示某列總計思路與代碼
- 嵌套repeater示例分享
相關(guān)文章
設(shè)置ASP.NET頁面的運行超時時間詳細到單個頁面及站點
這篇文章主要介紹了如何設(shè)置ASP.NET頁面的運行超時時間,包括全局超時時間、單個站點超時時間、單個頁面請求超時時間,需要的朋友可以參考下2014-06-06asp.net使用jquery模板引擎jtemplates呈現(xiàn)表格
這篇文章主要介紹了asp.net使用jquery模板引擎jtemplates呈現(xiàn)表格的示例,大家參考使用吧2014-01-01安裝.NET Framework進度條卡住不動的解決方案(推薦)
VS在安裝之前需要安裝.NET Framework,我安裝的是4.0版本。但是安裝進度條到一半左右時就卡住不動了。前前后后重試多次,還有幾次重新開機,但都沒用,怎么解決呢,下面給大家分享下解決方案2016-12-12如何輕松搭建Windows8云平臺的開發(fā)環(huán)境
Windows Store應(yīng)用是基于Windows 8操作系統(tǒng)的新一代Windows應(yīng)用程序,其開發(fā)平臺以及運行模式和以往傳統(tǒng)平臺略有不同。為了幫助更多開發(fā)人員加入到Windows Store應(yīng)用開發(fā)行列,本篇將介紹如何在Windows Azure云平臺搭建Windows8應(yīng)用開發(fā)環(huán)境,本篇介紹的方法適合未安裝Windows8操作系統(tǒng),使用Mac或者Linux平臺的開發(fā)人員參考閱讀。2013-02-02asp.net下將Excel轉(zhuǎn)成XML檔的實現(xiàn)代碼
通過Asp.net(C#)應(yīng)用程序讀取本地上傳的Excle文件,存放到DataSet中,通過DataSet中的方法直接生成XML文件.2009-11-11