. Net is currently a very popular programming language. Among the many knowledge points in. net training, it is very important to add five serial numbers to the Repeater control. The following is a brief introduction of this content by Mr. Dana.
Repeater is a data control that we often use to display datasets. We often want to display data sequence numbers before data. How can we add sequence numbers for the Repeater control? The following describes several common methods to add sequence numbers for the Repeater control:
Method 1:
Using the Container. ItemIndex attribute, the Code is as follows:
Copy codeThe Code is as follows:
<Itemtemplate>
<% # Container. ItemIndex + 1%>
</Itemtemplate>
Method 2:
Use the Repeater's Items. Count attribute. The Code is as follows:
Copy codeThe Code is as follows:
<Itemtemplate>
<% # This. Repeater. Items. Count + 1%>
</Itemtemplate>
Method 3:
Use JS to assign a Label value to the front-end. The Code is as follows:
Add a Label control in. aspx to display the serial number.
<Label ID = "label" runat = "server"> </Label>
JS Code:
Copy codeThe Code is as follows:
<Body onload = "show ()">
<Script Language = "javascript">
Function show ()
{
Var bj = document. all. tags ("Html Tag generated by Label after explanation ");
For (I = 0; I <obj. length; I ++)
{
Document. all ["Html Tag generated after Label explanation"] [I]. innerHTML = I + 1;
}
}
</Script>
This method requires a lot of attention and is not recommended.
Method 4: the code is as follows:
Add a Label control in. aspx
Copy codeThe Code is as follows:
<Asp: Label id = "Label1" runat = "server"> </asp: Label>
Add code in. cs:
Copy codeThe Code is as follows:
Void InitializeComponent ()
{
This. Repeater1.ItemDataBound + = new System. Web. UI. WebControls. RepeaterItemEventHandler (this. repeaterincluitemdatabound );
This. Load + = new System. EventHandler (this. Page_Load );
}
Void repeaterincluitemdatabound (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 consecutive numbers for the Repeater control. After turning pages, the serial number is followed by the serial number of the previous page. The Code is as follows:
Copy codeThe Code is as follows:
<% # Container. ItemIndex + 1 + (this. AspNetPager. CurrentPageIndex-1) * data volume per page>
The five methods for adding serial numbers to the Repeater control have been introduced by the internal training instructor. I hope this article will help students.