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

舊代碼重構(gòu)之后的代碼,第16行代碼,是判斷第一列,因?yàn)樗亲址?,因此單?dú)排除。第23行,使用ViewState來(lái)替代舊程序的5個(gè)變量。
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é)合實(shí)現(xiàn)分頁(yè)功能
- Repeater控件實(shí)現(xiàn)編輯、更新、刪除等操作示例代碼
- Repeater怎么實(shí)現(xiàn)多行間隔顯示分隔符
- Repeater中嵌套R(shí)epeater的示例介紹
- repeater做刪除前彈窗詢(xún)問(wèn)實(shí)例
- 給Repeater控件里添加序號(hào)的5種才常見(jiàn)方法介紹
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)(圖文詳解)
- .net JS模擬Repeater控件的實(shí)現(xiàn)代碼
- Repeater控件綁定的三種方式
- 利用js的Node遍歷找到repeater的一個(gè)字段實(shí)例介紹
- ASP.NET筆記之 Repeater的使用
- Repeater綁定dictionary數(shù)據(jù)源代碼及報(bào)錯(cuò)解決
- asp.net Repeater分頁(yè)實(shí)例(PageDataSource的使用)
- asp.net中使用repeater和PageDataSource搭配實(shí)現(xiàn)分頁(yè)代碼
- Repeater里switch的使用方法
- Repeater的FooterTemplate顯示某列總計(jì)思路與代碼
- 嵌套repeater示例分享
相關(guān)文章
設(shè)置ASP.NET頁(yè)面的運(yùn)行超時(shí)時(shí)間詳細(xì)到單個(gè)頁(yè)面及站點(diǎn)
這篇文章主要介紹了如何設(shè)置ASP.NET頁(yè)面的運(yùn)行超時(shí)時(shí)間,包括全局超時(shí)時(shí)間、單個(gè)站點(diǎn)超時(shí)時(shí)間、單個(gè)頁(yè)面請(qǐng)求超時(shí)時(shí)間,需要的朋友可以參考下2014-06-06asp.net使用jquery模板引擎jtemplates呈現(xiàn)表格
這篇文章主要介紹了asp.net使用jquery模板引擎jtemplates呈現(xiàn)表格的示例,大家參考使用吧2014-01-01ASP.NET頁(yè)面某些選項(xiàng)進(jìn)行提示判斷具體實(shí)現(xiàn)
針對(duì)asp.net頁(yè)面某些選項(xiàng)進(jìn)行提示判斷,比如當(dāng)保存一個(gè)信息時(shí)候,需要對(duì)當(dāng)前信息是否為空進(jìn)行判斷2013-06-06安裝.NET Framework進(jìn)度條卡住不動(dòng)的解決方案(推薦)
VS在安裝之前需要安裝.NET Framework,我安裝的是4.0版本。但是安裝進(jìn)度條到一半左右時(shí)就卡住不動(dòng)了。前前后后重試多次,還有幾次重新開(kāi)機(jī),但都沒(méi)用,怎么解決呢,下面給大家分享下解決方案2016-12-12如何輕松搭建Windows8云平臺(tái)的開(kāi)發(fā)環(huán)境
Windows Store應(yīng)用是基于Windows 8操作系統(tǒng)的新一代Windows應(yīng)用程序,其開(kāi)發(fā)平臺(tái)以及運(yùn)行模式和以往傳統(tǒng)平臺(tái)略有不同。為了幫助更多開(kāi)發(fā)人員加入到Windows Store應(yīng)用開(kāi)發(fā)行列,本篇將介紹如何在Windows Azure云平臺(tái)搭建Windows8應(yīng)用開(kāi)發(fā)環(huán)境,本篇介紹的方法適合未安裝Windows8操作系統(tǒng),使用Mac或者Linux平臺(tái)的開(kāi)發(fā)人員參考閱讀。2013-02-02WPF實(shí)現(xiàn)簡(jiǎn)單的跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)簡(jiǎn)單的跑馬燈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06asp.net下將Excel轉(zhuǎn)成XML檔的實(shí)現(xiàn)代碼
通過(guò)Asp.net(C#)應(yīng)用程序讀取本地上傳的Excle文件,存放到DataSet中,通過(guò)DataSet中的方法直接生成XML文件.2009-11-11