DataList Control
The DataList control is also a common data binding control. It is not as powerful as the GridView control, but flexible as it is. Because it is an elastic control. The DataList control allows you to use templates and definition styles to display data. You can select, delete, and edit data as needed.The templates supported by DataList are described as follows:
- AlternatingItemTemplate: If defined, the alternate items in DataList provide content and layout. If not defined, ItemTemplate is used.
- EditItemTemplate: If defined, the current edit item in DataList provides content and layout. If not defined, ItemTemplate is used.
- FoterTemplate: If defined, the content and layout of the current footer section in DataList are provided. If not defined, the footer section is not displayed.
- HeaderTemplate: If defined, the content and layout of the current header in DataList are provided. If not defined, the header is not displayed.
- ItemTemplate: templates required for content and layout in DataList
- SelectedItemTemplate: If defined, content and layout are provided for the current DataList option. If not defined, ItemTemplate is used.
- SeparatorTemplate: If defined, content and layout are provided for the separators between DataList items. If not defined, no separator is displayed.
The following experiment describes how to use SQL statements to query data from a database.
Create an ASP. NET Website and add a DataList control to the page to display paging data.
Then, edit the my DataList control as follows:
Edit the ItemTemplate option. The Code is as follows:
<Asp: dataList ID = runat => <ItemTemplate> <table> <tr style => <td rowspan = align => <a href => </a> </td> <td align => <a> </td> <td align => & nbsp; </td> <td> & nbsp; </td> </tr> <td align => <a> </td> <td align => -- Creation Time: <a> </td> <td> & nbsp; </td> </tr> <td align = colspan => <a> </td> </tr> </table> </ItemTemplate> <FooterTemplate> <div style => <table id = border = cellpadding = cellspacing => <tr> <td> <asp: label ID = runat => </asp: Label>/<asp: Label ID = runat => </asp: Label> <asp: linkButton ID = runat = CommandName = Font-Underline => homepage </asp: LinkButton> <asp: linkButton ID = runat = CommandName = Font-Underline => previous page </asp: LinkButton> <asp: linkButton ID = runat = CommandName = Font-Underline => next page </asp: LinkButton> <asp: linkButton ID = runat = CommandName = Font-Underline => last page </asp: LinkButton> & nbsp; jump to: <asp: textBox ID = runat = Width = Height => </asp: TextBox> <asp: button ID = runat = CommandName = Text =/> <br/> </td> </tr> </table> </div> </FooterTemplate> </asp: dataList>
Let's take a look at the test data in our database:
Then we need to implement a data paging function and bind the paging data to the DataList space.
The specific code is as follows:
/// <Summary> /// implement data paging // </summary> /// <param name = "currentpage"> </param> private void BindDataList (int currentpage) {PPS. allowPaging = true; // allows pagination of the PNS. pageSize = 4; // 4 pieces of data per page. currentPageIndex = currentpage; // the current page is an int type string querySql = string. format ("SELECT * FROM dbo. message "); connection. open (); SqlDataAdapter sda = new SqlDataAdapter (querySql, connection); DataSet ds = new DataSet (); // put the executed data in sda. fill (ds); // the data in the dataset is put into the page data. dataSource = ds. tables [0]. defaultView; this. dataList1.DataSource = PPS; this. dataList1.DataBind (); connection. close ();}
Then we need to trigger the ItemDataBound event of DataList to display the current page number and total page number in the control. Then, set whether the paging button is available:
Protected void DataList1_ItemCommand (object source, DataListCommandEventArgs e) {switch (e. commandName) {// The following five events are captured when a user clicks the next page of the previous page. currentPageIndex = 0; BindDataList (pds. currentPageIndex); break; case "pre": // the first page. currentPageIndex = pds. currentPageIndex-1; BindDataList (pds. currentPageIndex); break; case "next": // next page. currentPageIndex = pds. currentPageIndex + 1; BindDataList (pds. currentPageIndex); break; case "last": // the last page. currentPageIndex = pds. pageCount-1; BindDataList (pds. currentPageIndex); break; case "search": // if (e. item. itemType = ListItemType. footer) {int PageCount = int. parse (pps. pageCount. toString (); TextBox txtPage = e. item. findControl ("txtPage") as TextBox; int MyPageNum = 0; if (! TxtPage. text. equals ("") MyPageNum = Convert. toInt32 (txtPage. text. toString (); if (MyPageNum <= 0 | MyPageNum> PageCount) Response. write ("<script> alert ('enter the number of pages and make sure the total number of pages has not been exceeded! ') </Script> "); else BindDataList (MyPageNum-1);} break ;}}
Then, we continue the ItemCommand event of the DataList control. In this event, when the "homepage/Previous Page/next page/last page" button is clicked, in addition, you can simply enter the page Jump function in the text box:
Protected void DataList1_ItemDataBound (object sender, DataListItemEventArgs e) {if (e. item. itemType = ListItemType. footer) {// The following six are the controls in the get Script Template and create variables. label CurrentPage = e. item. findControl ("labCurrentPage") as Label; Label PageCount = e. item. findControl ("labPageCount") as Label; LinkButton FirstPage = e. item. findControl ("lnkbtnFirst") as LinkButton; LinkButton PrePage = e. item. findControl ("lnkbtnFront") as LinkButton; LinkButton NextPage = e. item. findControl ("lnkbtnNext") as LinkButton; LinkButton LastPage = e. item. findControl ("lnkbtnLast") as LinkButton; CurrentPage. text = (pps. currentPageIndex + 1 ). toString (); // bind to display the current page PageCount. text = PPS. pageCount. toString (); // The total number of bound pages. isFirstPage) // if it is the first page, the homepage and the previous page cannot use {FirstPage. enabled = false; PrePage. enabled = false;} if (pds. isLastPage) // if the last page is "next page" or "last page", {NextPage. enabled = false; LastPage. enabled = false ;}}}
The complete code is as follows:
PagedDataSource PPS = PagedDataSource (); SqlConnection connection = SqlConnection (Page_Load ((! BindDataList (=; Pam. pageSize =; PPS. currentPageIndex = currentpage; querySql =. format (= PPS. dataSource = ds. tables [. dataList1.DataSource = DataList1_ItemCommand (: pds. currentPageIndex =: PPS. currentPageIndex = pds. currentPageIndex-: PPS. currentPageIndex = pds. currentPageIndex +: pds. currentPageIndex = pds. pageCount-: (e. item. itemType = PageCount = e. item. findControl () MyPageNum = (! TxtPage. text. equals (= (MyPageNum <= | MyPageNum>-DataList1_ItemDataBound (e. item. itemType = Label CurrentPage = e. item. findControl () = e. item. findControl () = e. item. findControl () = e. item. findControl () = e. item. findControl () = e. item. findControl () = (pps. currentPageIndex + ). toString (); PageCount. text = PPS. pageCount. toString (); (pps. isFirstPage) = (pps. isLastPage) =View Code
The program running result is as follows:
For more information about DataList, see the following section.
Event |
Description |
CancelCommand |
An error occurred while clicking the Cancel button for an item in the DataList control. |
DeleteCommand |
An error occurred while clicking the Delete button on an item in the DataList control. |
EditCommand |
An error occurred while clicking the Edit button for an item in the DataList control. |
ItemCommand |
This occurs when any button of the DataList control is clicked. |
ItemDataBound |
Occurs when the item is bound to the DataList control. |
SelectedIndexChanged |
This occurs when different items are selected in the data list control between two service senders. |
UpdateCommand |
An error occurred while clicking Update on an item in the DataList control. |