5 ways to add serial numbers to the Repeater control Introduction _ Practical Tips

Source: Internet
Author: User
. NET is a very popular programming language at the moment, and in the many knowledge points of. NET training, the 5 methods of adding serial numbers to the Repeater control are very important. The following is the the teacher to introduce you to this aspect of the content.

Repeater is a data control that we often use to display datasets, and often we want to display the number of data before the data, so how do we add an ordinal number to the Repeater control? The following editor introduces several common ways to add an ordinal number to a Repeater control:

Method One:
Using the Container.itemindex property, the code is as follows:
Copy Code code as follows:

<itemtemplate >
<%# Container.itemindex + 1% >
</itemtemplate >

Method Two:
Using the Repeater Items.Count attribute, the code is as follows:
Copy Code code as follows:

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

Method Three:
Use JS to assign a value to a label label in the foreground, the code is as follows:

Adds a label control to the. aspx to display the ordinal number.

<label id= "Label" runat= "Server" ></label >

JS Code:
Copy Code code as follows:

<body onload= "Show ()" >
<script language= "JavaScript" >
Function Show ()
{
var bj = document.all.tags ("HTML label generated after the label is interpreted");
for (i=0;i<obj.length;i++)
{
document.all["label interpreted HTML tag"][i].innerhtml=i+1;
}
}
</script >

This method needs to pay attention to the place more, does not recommend the use.

Method Four: Implemented in the background, the code is as follows:
Add a Label control in. aspx
Copy Code code as follows:

<asp:label id= "Label1" runat= "Server" ></asp:label >

Add code in. CS:
Copy Code code as follows:

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);
}
}

Method Five: Add a sequential number for the Repeater control, and the number of the previous page after the paging sequence, the code is as follows:
Copy Code code as follows:

<%# Container.itemindex + 1 + (this. ASPNETPAGER.CURRENTPAGEINDEX-1) * Number of data per page >

The content of the 5 methods for adding serial numbers to the Repeater control has been introduced by the instructor of the the training, and it is hoped that the contents of this article will be helpful to the students.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.