. 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.