Auto-numbered DataGrid (Golden Edition)

Source: Internet
Author: User

Automatic numbering is a lot of useful. You can view the total number of data records and name the automatically generated Div and HTML controls. This control can be accurately operated, dataGrid, datalist, repeater, and other binding controls can be used for convenience and practicality.

I. Forward order

Serial number content
1
2 B
3 C
4 d

A. When allowpaging = false

<Asp: DataGrid id = "datagrid1" runat = "server">
<Columns>
<Asp: templatecolumn>
<Itemtemplate>
<% # Container. itemindex + 1%>
</Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid>
 

Recommended Methods:
<Asp: DataGrid id = "datagrid1" runat = "server">
<Columns>
<Asp: templatecolumn>
<Itemtemplate>
<% # This. datagrid1.items. Count + 1%>
</Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid>

From the above point of view, it is bound in the itemcreated event, so the resulting items. Count is the current serial number.]

B. Under allowpaging = "true"
If your DataGrid supports pagination, you can:

<Asp: DataGrid id = "datagrid1" runat = "server" allowpaging = "true">
<Columns>
<Asp: templatecolumn>
<Itemtemplate>
<% # This. datagrid1.currentpageindex * This. datagrid1.pagesize + container. itemindex + 1%>
</Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid>

C. Generate the. CS file in the background

Private void maid (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
// When the item is bound to the DataGrid Control, the itemdatabound event is triggered.
If (E. Item. itemindex! =-1)
{
E. Item. cells [0]. Text = (this. Maid + E. Item. itemindex + 1). tostring ();
}
}

Ii. Reverse Order Method

Serial number content
4 d
3 C
2 B
1

As you can see above
This. The datagrid1.items. Count-container. itemindex + 1 method is impossible. The obtained value is 1
The same is true for paging. Therefore, we need to obtain the number of rows of the data source at the beginning.

. CS:
 
Private int rowscount = 0;
Protected int rowscount
{

Get {return rowscount ;}
Set {This. rowscount = value ;}
}

Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
This. binddata (); // Data Binding
}

. Aspx:

<Asp: DataGrid id = "datagrid1" runat = "server" allowpaging = "true">
<Columns>
<Asp: templatecolumn>
<Itemtemplate>
<% # Rowscount-maid. currentpageindex * maid %>
</Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid>
Of course, it is easier to implement paging.

Example 3
Function: The ID of the DIV is automatically generated and hidden and displayed.
JS:

Function showscontent (objhref, pid)
{
VaR divid = 'div '+ PID;
VaR OBJ = Document. getelementbyid (divid );
If (obj. style. Display = "NONE ")
{
OBJ. style. Display = "Block ";
Objhref. innerhtml = "hide information ";
}
Else
{
OBJ. style. Display = "NONE ";
Objhref. innerhtml = "Basic Information ";
}
}
Aspx:
<Asp: datalist id = "datalist1" runat = "server" width = "630px" datakeyfield = "PID" showfooter = "false" showheader = "false" repeatdirection = "horizontal" repeatcolumns = "1 ">
<Itemtemplate>
<Table Height = "37" cellspacing = "0" cellpadding = "0" width = "630" align = "center" border = "0">
<Tr>
<TD> <SPAN class = "vlink" onclick = 'javascript: showscontent (this, <% # container. itemindex %>) '>
Basic Information </span> </TD>
</Tr>
<Tr>
<TD style = "color: # 827f7f" colspan = "2" valign = "TOP" align = "Left">
<Div style = "line-Height: 18px" id = 'div <% # container. itemindex %> 'style = "display: none"> & nbsp; <% # (datarowview) container. dataitem) ["pname"] %> <br>
& Nbsp; tag Tag: <% # (datarowview) container. dataitem) ["Pcontent"] %> </div>
</TD>
</Tr>
<Tr>
<TD colspan = "2" bgcolor = "# ececec" Height = "1" width = "100%">
</TD>
</Tr>
</Table>
</Itemtemplate>
</ASP: datalist>

. CS:

Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
This. binddata (); // Data Binding
}

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.