Using ASP. NET 3.5 ''s listview and datapager controls: Grouping

Source: Internet
Author: User
A multipart series on ASP. NET's listview and datapager controls
A series of ASP. NET listvie and datapager controls
This article is one in a series of articles on ASP. net's listview and datapager controls, which were introduced with ASP. net version 3.5.
This Article is about ASP.. Net listvie and datapager controls, using ASP.. Net 3.5.

  • displaying data with the listview-looks at the listview control basics, with demos on how to display data using the layouttemplate and itemtemplate.
    displaying data with the listview-focus on the basics of listview, use demo to demonstrate how to use layouttepmlate and itemtepmlate to display data
  • grouping data with the listview control-shows how to render different formatting or encasing markup to every n rendered records.
    grouping data with the listview control-describes how to present different formats and packaging IDs for each n presented records
  • Introduction
    ASP. net version 3.5 added two new data Web controls to the toolbox: The listview and datapager. as discussed in the first installment of this article series, displaying data with the listview, The listview control offers the same built-in features found in the gridview, but much finer control over the rendered output. the listview's output is defined using a variety of templates, and we looked at examples using the control's layouttemplate and itemtemplates. in particle, these examples used a layouttemplate that has ded a placeholder for the itemtemplate's rendered output.

    Asp.net 3.5 adds a new data control in toolbox: listview and datapager. in this article, the first part of the column (displaying data with the listview) is discussed. The listview control provides some built-in features of the gridview and provides better control presentation. The rendering of listview is defined using multiple templates, and we are concerned with examples of controls using layouttemplate and itemtemplaet. Specifically, layouttepmplate containing the plachholder control is used for the itemtemplate presentation.

    the itemtemplate is rendered for each record bound to the listview control, and is typically referenced in the layouttemplate. this approach generates the rendered markup defined in the layouttemplate, plus the rendered markup created by the itemtemplate for each record. this works fine for simple rendering scenarios, but in more complex scenarios we may need to render different formatting markup for different groups of records. for example, imagine that we needed to display a set of records in a three-column HTML

    groupitemcount

    . for each record we wowould want to emit a table cell ( ). such customizations can be accomplished declaratively with the listview control's supported des grouptemplate and properties.
    ), but for every three records we wocould need to emit a new table row (

    Itemtemplate presents the output of each record bound to listview and is referenced in layouttepmplate. This step generates the identifier defined in layouttepmlate. add itemptemplate to the output ID created for each record. this can be done well for simple output requirements, but in more flexible requirements, we may need to present different formats of identifiers for different record groups. For example, imagine that we need to display a group of records in a three-column <Table>. We want to display a table cell (<TD>) for each record, but we need to display a new table row (<tr>) for each of the three records ). you can use the grouptemplate and grouipitemcount attribute declarations that come with the listview control to complete this customization.

    In this article we will see how to use the grouptemplate andGroupitemcountProperties to instruct the listview control to render different encasing markup for everyNRecords. we will look at two demos: one that renders records into a series of ordered lists, and another that has strates how to display data in a multi-column table. read on to learn more!

    In this article, we will see how to use the grouptemplate and groupitemgcount attributes to notify the listview control to generate different include identifiers for each n records. We will focus on two demos: one is to display records into a series of order lists, and the other is to show how data is displayed in a multi-column table.

    -Continued-



     

    Grouping Basics
    As we saw in the displaying data with the listview article, the listview control contains two requisite templates: layouttemplate and itemtemplate. the layouttemplate is rendered to generate the listview control's markup and can contain in a reference to the itemtemplate, which is used to render each record bound to the listview control. the layouttemplate references the itemtemplate through a server-side control (such as the placeholder Control) whoseIDIs the same as the listview'sItemplaceholderidProperty. (ItemplaceholderidProperty has a default value of "itemplaceholder ".)

    Group Basics
    As in grouptemplate and setting the listview control'sGroupitemcountPropertyN. Then, instead of referencing the itemtemplate in the layouttemplate, have the layouttemplate reference the grouptemplate, And the grouptemplate reference the itemtemplate. such a setup still has the itemtemplate rendered for every record bound to the listview control, but causes the grouptemplate to be rendered everyGroupitemcountNumber of records.

    If each item contains the same identifiers, the reference from layouttemplate to itemlate is very good, but in some ideas, different include identifiers may need to be displayed every n items. In this way, you can define a grouptemolate and set the groupitemcount attribute of the listview control to n. in layouttemplate, the referenced itemtemplate is replaced. layouttemplate references grouptemplate and grouptemolate references itemtemplate. in this way, the itemptemplate is still generated for the discipline bound to the listview control, but the grouptemplate will be generated for the number of records per groupitemcount.

    To better understand how grouping with the listview control works, let's take the first example we examined in the displaying data with the listview article and extend it to use grouping. the first example was strated how to display a set of records in an ordered list, and used the following declarative markup for the listview control:

    To better understand how the group using the listview control works, let's take the first example analyzed in the displaying data with the listview article and extend it using the group. The first column describes how to display a group of data in an order list. The listview control uses the following declared identifier:

    <Asp: listview id = "..." runat = "server" performanceid = "..."> runat = "server" performanceid = "...">
    <Layouttemplate>
    <Ol>
    <Asp: placeholder runat = "server" id = "itemplaceholder"> </ASP: placeholder>
    </OL>
    </Layouttemplate>

    <Itemtemplate>
    <Li> <% # eval ("Columnname") %> </Li & </ASP: listview>

    Imagine, however, that we want to display an ordered list for each group of three records. To accomplish this use the following markup instead:

    We want to display an order list with three records in each group. To meet this requirement, use the following identifier instead:

    <Asp: listview id = "productlist1" runat = "server" id = "productlist1" runat = "server"
    Performanceid = "productdatasource"
    Groupitemcount = "3"Itemplaceholderid = "itemsgohere"
    Groupplaceholderid = "groupsgohere">

    <Layouttemplate>
    <P>
    <Asp: placeholder runat = "server" id = "groupsgohere"> </ASP: placeholder>
    </P>
    </Layouttemplate>








  • <% # eval ("productname ") %>

  • The listview control's declarative markup is nearly the same as in the previous article, but instead of<Ol>Element being in the layouttemplate, it has been moved to the grouptemplate. the itemtemplate has reamined the same. note, however, that the layouttemplate still must be present and now references the grouplayout. also note that instead of the default values for the group and item placeholders ("groupplaceholder" and "itemplaceholder") I have explicitly changed these values through the listview control'sItemplaceholderidAndGroupplaceholderidProperties.

    The declared identity of the listview control is almost the same as that of the previous article, but the <ol> element in the layouttepmplate is replaced, and it is already transferred to the grouptemplate. The itemtemplate remains the same. Note: The layouttemplate still must appear, but the grouplayout is referenced now. also, replace the default values for group and placeholder items ("groupplaceholder" and "itemplaceholder"). I have explicitly changed these values through the itemplaceholderid and groupplaceholderid attributes of the listview control.

    Imagine that the above listview is bound to an employees database table, and that in the itemtemplate we were renderingFullnameColumn within<Li>Element. What wocould the listview's rendered markup look like?

    Assume that the previous listview is bound to an emplaoyees database table, and the fullname column in The itemtemplate is displayed in the <li> element. What does the listview rendering look like?

    Well, the listview wocould start by rendering it's layouttemplate:

    Okay, listview should first start to render its layouttepmlate:

    <P><P>
    <Asp: placeholder runat = "server" id = "groupsgo

    It wocould then render itsGrouptemplateFor each group of three records bound to the listview control. Assuming that there were eight total employees, this wocould result in the following markup:

    A group of listview records bound to each of the three listview controls will display its grouptemplate. Suppose we have eight complete employees, which is the result of the following identification:

    <Ol> ode><Ol>
    <Asp: placeholder runat = "server" id = "itemsgohere"> </ASP: placeholder>
    </OL>

    <Ol>
    <Asp: placeholder runat = "server" id = "itemsgohere"> </ASP: placeholder>
    </OL>

    <Ol>
    <Asp: placeholder runat = "server" </OL>

    It wocould then render itsItemtemplateFor each record bound to the listview control. This might result in the following markup:

    For each record bound to the listview control, listview will display its itemptemplate. The possible result will be the following identifier:

    <Li> Scott Mitchell </LI> avy "> <li> Scott Mitchell </LI>
    <Li> Sam Smith </LI>
    <Li> jisun Lee </LI>
    <Li> Andrew fuller </LI>
    <Li> Edgar Johnson </LI>
    <Li> Ellen plank </LI>
    <Li> Tito Wald & L

    The itemtemplates 'rendered markuitemtemplate presents the identifier to the appropriate position of the grouplayout placeholder control. The final result is as follows:

    TH =" 95% "border = 0>



    1. Scott Mitchell

    2. Sam Smith

    3. jisun Lee



    1. Andrew fuller

    2. Edgar Johnson

    3. Ellen plank



    1. Tito

    Displaying data in a multi-column table
    Displaying records in a multi-column table is a very common scenario, and is one that requires rendering different formatting markup for groups of records. oftentimes, such formatting is achieved through the use of a multi-column html<Table>. For example, to display a three-column table, we wocould render three table cells (<TD>) In each table row (<Tr>), Like so: displays records in a multi-list
    Displaying Data in multiple lists is a very common scenario, and it is also required to present different format identifiers for record groups. Generally, these formats can be completed by using HTML <Table> with multiple columns. For example, to display a 3 list, we will present 3 table cells (TD) in each tablerow (<tr>), like this:

     

     


    Copyright 2013 Cao Zhenhua

    Posted @Cao Zhenhua (...) Comment (...) EDIT favorites

    Refresh comment refresh page Back to Top

    Blog homepage blog news flash programmer recruitment Knowledge Base


    Announcement



    <Table...> CCC><Table...> row ( <Tr> ), Like so:

    <Table...>
    <Tr>
    <TD> record 1 </TD>
    <TD> record 2 </TD>
    <TD> record 3 </TD>
    </Tr>

    ...

    <Tr>
    <TD> record N-</table>

    In order to generate such markup with a listview control, we need to use a layouttemplate that renders the outer<Table>And</Table>Tags, A grouptemplate that renders the table row elements, and an itemtemplate that renders each table cell. The following declarative markup extends strates how to display data in a three-column table:

    To use the listview control to generate such an identifier, we need to use a layouttemplate to present<Table>And</Table>A grouptemplate is used to present the table row element and an itemtemplate is used to present a table cell.
    The following statement shows how to display data in a 3 list.

    <Asp: listview id = "productdatalist2" runat = "server" n a three-column table:

    <Asp: listview id = "productdatalist2" runat = "server"
    Performanceid = "..." groupitemcount = "3">

    <Layouttemplate>
    <Table>
    <Tr>
    <TD>
    <Table border = "0" cellpadding = "5">
    <Asp: placeholder runat = "server" id = "groupplaceholder"> </ASP: placeholder>
    </Table>
    </TD>
    </Tr>
    </Table>
    </Layouttemplate>

    <Grouptemplate>
    <Tr>
    <Asp: placeholder runat = "server" id = "itemplaceholder"> </ASP: placeholder>
    </Tr>
    </Grouptemplate>

    <Itemtemplate>
    <TD>
    <H2> <% # eval ("productname") %> </H2>

    Price: <% # eval ("unitprice", "{0: c}") %> <br </ASP: listview>

    The download available at the end of this article between des a demo that displays the records from the northwind database'sProductsTable in a three-column table, which yields the following when viewed through a browser.

    A useful download at the end of the article contains a demo showing the data from the product table of the northwind database in the 3 list. The following page appears when you access the product table through the browser:

    conclusion
    In this article we examined how to render output batched into a specified group size using the listview control's grouptemplate and groupitemcount property. this template and property are useful in scenarios where the formatting layout needs to change for every n rendered records, such as when displaying a multi-column table. prior to the lis Tview control, such grouping required the page developer to write code that wocould intelligently inject additional formatting markup for every n records. but with the listview control's grouptemplate and groupitemcount property, group formatting is now possible entirely through declarat
    conclusion
    This article describes how to use the grouptemplate and groupitemcount attributes of the listview control to render the output in a specified group. These templates and attributes are useful in the format layout for scenarios where each output needs to be changed. For example, we can display a table with multiple columns. the listview control is in front, for requirements such as grouping, developers need to write Code that can intelligently insert additional format identifiers for each n record pages. However, you can declare the Group format by using the grouptemplate and groupitemcount attributes of the listview control.

    Happy programming!

    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.