溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Repeater控件動(dòng)態(tài)變更列的示例分析

發(fā)布時(shí)間:2021-10-18 09:44:38 來源:億速云 閱讀:124 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)Repeater控件動(dòng)態(tài)變更列的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。


重構(gòu)開始:
首先把這五個(gè)變量刪除,因?yàn)樵谥貥?gòu)過程中,已經(jīng)不需要這五個(gè)變量了。

復(fù)制代碼 代碼如下:


//宣告5個(gè)變量,將用來存儲(chǔ)那5個(gè)月份每個(gè)部分的數(shù)量
decimal c1, c2, c3, c4, c5;


接下來需要改動(dòng)的是宣告一個(gè)常量,很多地方使用到它:

復(fù)制代碼 代碼如下:


const int dynamicColumns = 5;


把程序中的下面這句

復(fù)制代碼 代碼如下:


objPrintLog.Months = 5; //最近連續(xù)5個(gè)月份


改為:

復(fù)制代碼 代碼如下:


objPrintLog.Months = dynamicColumns;


也就是說,使用常量的變量去替代舊代碼的"5"。

接下來,我們重構(gòu)Repwater控件的Header的代碼,為了好對(duì)比,Insus.NET把上一篇對(duì)應(yīng)的圖片引用在這里:
Repeater控件動(dòng)態(tài)變更列的示例分析

 Repeater控件動(dòng)態(tài)變更列的示例分析

重構(gòu)如下:

復(fù)制代碼 代碼如下:


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 部分:
Repeater控件動(dòng)態(tài)變更列的示例分析

舊代碼重構(gòu)之后的代碼,第16行代碼,是判斷第一列,因?yàn)樗亲址?,因此單?dú)排除。第23行,使用ViewState來替代舊程序的5個(gè)變量。

復(fù)制代碼 代碼如下:


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):
Repeater控件動(dòng)態(tài)變更列的示例分析

Foot重構(gòu)好的代碼,第14行是判斷是否為第一列,第17行,是把ViewState的值賦給Label。

復(fù)制代碼 代碼如下:


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)是在程序功能要求不變的情況之下,減少冗余的代碼。

關(guān)于“Repeater控件動(dòng)態(tài)變更列的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI