給Repeater控件里添加序號(hào)的5種方法

2022-06-11    分類: 網(wǎng)站建設(shè)

Repeater是我們經(jīng)常用的一個(gè)顯示數(shù)據(jù)集的數(shù)據(jù)控件,經(jīng)常我們希望在數(shù)據(jù)前顯示數(shù)據(jù)的序號(hào),那么我們?cè)撛趺礊镽epeater控件添加序號(hào)呢?下面編輯為大家介紹幾種常用的為Repeater控件添加序號(hào)的方法:

方法一:
利用Container.ItemIndex屬性,代碼如下:

<Itemtemplate >
<%# Container.ItemIndex + 1% >
</Itemtemplate >

方法二:
利用Repeater的Items.Count屬性,代碼如下:

<Itemtemplate >
<%# this.Repeater.Items.Count + 1% >
</Itemtemplate >

方法三:
利用JS在前臺(tái)給一個(gè)Label標(biāo)簽賦值,代碼如下:

在.aspx中添加一個(gè)Label控件,用來(lái)顯示序號(hào)。

<Label ID='label' runat='server' ></Label >

JS代碼:

<body onload='show()' >
<Script Language='javascript' >
function show()
{
var bj = document.all.tags('Label經(jīng)解釋后生成的Html標(biāo)簽');
for (i=0;i<obj.length;i++)
{
document.all['Label經(jīng)解釋后生成的Html標(biāo)簽'][i].innerHTML=i+1;
}
}
</script >

該方法需注意的地方比較多,不推薦使用。

 方法四:在后臺(tái)中實(shí)現(xiàn),代碼如下:
在.aspx里添加一個(gè)Label控件

<asp:Label id='Label1' runat='server' ></asp:Label >

在.cs里添加代碼:

void InitializeComponent()
{  
   this.Repeater1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound);
   this.Load += new System.EventHandler(this.Page_Load);
}
void Repeater1_ItemDataBound(object source, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
    ((Label)e.Item.FindControl('Label1')).Text = Convert.ToString(e.Item.ItemIndex + 1);
   }
}

方法五:為Repeater控件添加連續(xù)的編號(hào),翻頁(yè)后序號(hào)接前上一頁(yè)的序號(hào),代碼如下:

<%# Container.ItemIndex + 1 + (this.AspNetPager.CurrentPageIndex -1)*每頁(yè)的數(shù)據(jù)數(shù)量 >

新聞標(biāo)題:給Repeater控件里添加序號(hào)的5種方法
文章URL:http://www.bm7419.com/news3/166003.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站移動(dòng)網(wǎng)站建設(shè)、定制開發(fā)、搜索引擎優(yōu)化、域名注冊(cè)、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司