Five common ways to add a sequence number to an Repeater control

  • 2020-07-21 07:30:44
  • OfStack

.net is one of the most popular programming languages, and among the many knowledge points in.ES1en training, five ways to add sequence Numbers to Repeater controls are very important. Danet's teacher will introduce the content of this topic.

Repeater is a frequently used data control that displays a data set. Often we want to show the number of the data before the data, so how do we add the number to the Repeater control? Here are some common ways to add sequence Numbers to Repeater controls:

Method 1:
Using the Container. ItemIndex attribute, the code is as follows:
 
<Itemtemplate > 
<%# Container.ItemIndex + 1% > 
</Itemtemplate > 

Method 2:
Using the Items. Count attribute of Repeater, the code is as follows:
 
<Itemtemplate > 
<%# this.Repeater.Items.Count + 1% > 
</Itemtemplate > 

Method 3:
Use JS to assign value to an Label tag in the foreground. The code is as follows:

Add 1 Label control to.aspx to display serial Numbers.

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

JS code:
 
<body onload="show()" > 
<Script Language="javascript" > 
function show() 
{ 
var bj = document.all.tags("Label Generated by interpretation Html The label "); 
for (i=0;i<obj.length;i++) 
{ 
document.all["Label Generated by interpretation Html The label "][i].innerHTML=i+1; 
} 
} 
</script > 

This method needs to pay attention to more places, not recommended.

Method 4: Implement in the background with the following code:
Add 1 Label control to.aspx
 
<asp:Label id="Label1" runat="server" ></asp:Label > 

Add code to.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); 
} 
} 

Method 5: Add a serial number to the Repeater control. After turning the page, the serial number is followed by the serial number of the previous page. The code is as follows:
 
<%# Container.ItemIndex + 1 + (this.AspNetPager.CurrentPageIndex -1)* The amount of data per page  > 

The five ways to add sequence Numbers to Repeater controls has been explained by dane-trained instructors, and I hope this article has been helpful to students.

Related articles: