給Repeater控制項裡添加序號的5種才常見方法介紹_實用技巧

來源:互聯網
上載者:User
.net是目前非常熱門的一種程式編譯語言,在.net培訓中的眾多知識點中,給Repeater控制項裡添加序號的5種方法是非常重要的一個。下面就由達內的老師為大家介紹一下這方面的內容。

Repeater是我們經常用的一個顯示資料集的資料控制項,經常我們希望在資料前顯示資料的序號,那麼我們該怎麼為Repeater控制項添加序號呢?下面編輯為大家介紹幾種常用的為Repeater控制項添加序號的方法:

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

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

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

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

方法三:
利用JS在前台給一個Label標籤賦值,代碼如下:

在.aspx中添加一個Label控制項,用來顯示序號。

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

JS代碼:
複製代碼 代碼如下:

<body onload="show()" >
<Script Language="javascript" >
function show()
{
var bj = document.all.tags("Label經解釋後產生的Html標籤");
for (i=0;i<obj.length;i++)
{
document.all["Label經解釋後產生的Html標籤"][i].innerHTML=i+1;
}
}
</script >

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

方法四:在後台中實現,代碼如下:
在.aspx裡添加一個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控制項添加連續的編號,翻頁後序號接前上一頁的序號,代碼如下:
複製代碼 代碼如下:

<%# Container.ItemIndex + 1 + (this.AspNetPager.CurrentPageIndex -1)*每頁的資料數量 >

關於給Repeater控制項裡添加序號的5種方法的內容已經由達內培訓的講師為您介紹完了,希望本文的內容能夠對學員們有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.