Listview Control in Asp.Net 3.5

ListView control is a super-flexible GridView control. Like a GridView control, the ListView control can be used to display, edit, delete, select, page through, and sort any data source data, but the ListView control is entirely template driven. You can insert the new data into data source by useing ListView control, you cannot insert the data using Gridview control.

The ListView control is a replacement for the DataList control. Like a DataList control, the ListView control can be used to display database records in multiple columns. For example, you can use the ListView control to render a photo gallery.

And also ListView control is a advanced Repeater control. Like a Repeater control, the ListView control is entirely template driven. Unlike a Repeater control, the ListView control can be used to edit, page through, and sort data source
data.

Below are the templates that are supported by ListView control:

LayoutTemplate—It is used to specify the containing element for the contents of the ListView.

ItemTemplate—It is used to format each item rendered by the ListView.

ItemSeparatorTemplate—It is used to display content between each item rendered by the ListView.

GroupTemplate—It is used to specify the containing element for a group of items rendered by the ListView.

GroupSeparatorTemplate—It is used to display content between each group of items rendered by the ListView.

EmptyItemTemplate—It is used to render content for the remaining items in a GroupTemplate.

EmptyDataTemplate—It is used to specify content that is displayed when no items are returned from the ListView control’s data source.

SelectedItemTemplate—It is used to specify the content displayed for the selected item in the ListView.

AlternatingItemTemplate—It is used to render different content for alternating items in a ListView.

EditItemTemplate—It is used to render content for editing an item in a ListView.

InsertItemTemplate—It is used to render content for inserting a new item in a ListView.


Here, I am explaining how to display the XML file data using Listview Control.

 

Default.aspx

 

<asp:ListView ID="listView" runat="server">
            <LayoutTemplate>
                <table>
                     <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>                   
                </table>
            </LayoutTemplate>
            <ItemTemplate>
             <tr>
                <td>
                    <%# Eval("Id") %>
                </td>
                <td>
                    <%# Eval("Name") %>
                </td>
             </tr>
            </ItemTemplate>

</asp:ListView>

 

Default.aspx.cs


protected void Page_Load(object sender, EventArgs e)

{
       BindListView();
}
private void BindListView()
{
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("Employee.xml"));
            listView.DataSource = ds.Tables[0].DefaultView;
            listView.DataBind();

}

As we discussed, Listview control is template driven. In listview control LayoutTemplate is used to define in which format data has to display. Here LayoutTemplate contains the table and ItemTemplate. ItemTemplate defines actual data from data source and displays in LayoutTemplate.

                                                                                                             aspnetListView.zip (17.66 kb)