CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Using GroupTemplate in ASP.Net ListView Control

By Satheesh Babu
Posted On Apr 11,2010
Article Rating:
Be first to rate
this article.
No of Comments: 7
Category: ASP.Net
Print this article.

Using GroupTemplate in ASP.Net ListView Control(Tiled Display)

 

ListView is a new databound control that is shipped with ASP.Net 3.5. ListView control is a powerful control which can replace all the other data bound controls available now. Read my previous article ListView Control in ASP.Net 3.5 which discusses the basic usage of ListView control in ASP.Net.

 

Moving forward, we will see how to use GroupTemplate for grouping data using ListView control. By using GroupTemplate we can display data in tiled format using ListView control.

 

Refer the below figure,

 

 

Figure 1 – Tiled Layout using ListView control

Grouping Data By Using Tiled Layout in ListView in ASP.Net 3.5

 

As i said in my previous article, we need to define the LayoutTemplate and ItemTemplate at the minimum to display data using ListView control. If we want to group data, then additionally we need to define the GroupTemplate and specify the GroupItemCount property of ListView control. We will understand this in coming sections.

 

GroupTemplate

The GroupTemplate will dictate how the data can be grouped. For example, to group data like the above figure we should define a TR tag with 3 TD’s. In this case, the grouping element will be TR. Thus, GroupTemplate should have TR which in turn should populate the items in TD using ItemTemplate.

 

GroupItemCount

This property will dictate the number of items to repeat in each group. In our example, it is 3 TD’s.

We can understand this when we build a sample ListView control with grouping. Next section will help you to do that.

 

Steps

1.      You can create a new Asp.Net website project with C# as language in your Visual Studio 2008.

2.      Include a new SQL express database inside App_Data folder and create a table called Users with necessary columns.

Refer the below figure,

Figure 2 – Server Explorer

Grouping Data By Using Tiled Layout in ListView Control in ASP.Net 3.5

3.      Drag a ListView control and SqlDataSource control from the data tab of visual studio.

4.      Configure the SqlDataSource control to fetch the Employee table data from the Sql Express database under App_Data folder.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

        SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource>

Web.config

<connectionStrings>

              <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

</connectionStrings>

 

5.      As is said earlier, to display a tiled layout(Grouping) we need to design the GroupTemplate additionally to LayoutTemplate and ItemTemplate. Refer the below code,

<asp:ListView ID="lvEmp" runat="server"

            DataSourceID="SqlDataSource1" GroupItemCount="3">

            <LayoutTemplate>

            <table>

                 <tr ID="groupPlaceholder" runat="server">

                 </tr>

            </table>

            </LayoutTemplate>

            <GroupTemplate>

                    <tr>

                        <td ID="itemPlaceholder" runat="server">

 

                        </td>

                    </tr>

             </GroupTemplate>

             <ItemTemplate>

                    <td align="left">                  

                     <b>EMPID:</b> <asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>' /><br />

                     <b>Name:</b> <asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' /><br />

                     <b>Department:</b> <asp:Label ID="DepartmentLabel" runat="server" Text='<%# Eval("Department") %>' /><br />

                     <b>Age:</b> <asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' /><br />

                     <b>Address:</b> <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />

                    </td>               

             </ItemTemplate>

            </asp:ListView>

 

In the above code, the ItemTemplate content will be repeated 3 times(GroupItemCount) inside the GroupTemplate element “itemPlaceholder and it will be then copied to the LayoutTemplate’s groupPlaceholder element.

6.      Execute the page and you can see the Listview grouping in action.

7.      Now, to get the line just after every row (group) we need define an additional template called GroupSeparatorTemplate.

Refer below,

<GroupSeparatorTemplate>

              <tr runat="server">

                <td colspan="3"><hr /></td>

              </tr>

 </GroupSeparatorTemplate>

 




The contents of this template will be emitted after every group in the output html.

Execute the project by pressing F5.

 

In the above code, we have designed the ListView grouping using table tag. The below code can be used to achieve the same using DIV tag.

 

<asp:ListView ID="ListView1" runat="server"

            DataSourceID="SqlDataSource1" GroupItemCount="3" GroupPlaceholderID="groupPlaceholder1" ItemPlaceholderID="itemPlaceholder1">

            <LayoutTemplate>

            <div>

                 <div ID="groupPlaceholder1" runat="server">

                 </div>

            </div>

            </table>

            </LayoutTemplate>

            <GroupTemplate>

                    <div style="clear:both">

                        <div ID="itemPlaceholder1" runat="server">

 

                        </div>

                    </div>

             </GroupTemplate>

             <ItemTemplate>

                    <div style="float:left">                  

                     <b>EMPID:</b> <asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>' /><br />

                     <b>Name:</b> <asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' /><br />

                     <b>Department:</b> <asp:Label ID="DepartmentLabel" runat="server" Text='<%# Eval("Department") %>' /><br />

                     <b>Age:</b> <asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' /><br />

                     <b>Address:</b> <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />

                    </div>               

             </ItemTemplate>

             <GroupSeparatorTemplate>

              <div runat="server">

                <div style="clear:both"><hr /></div>

              </div>

            </GroupSeparatorTemplate>

            </asp:ListView>

 

Note

If you specify the group placeholder id as groupPlaceholder and item placeholder id as itemPlaceholder then you need not define the GroupPlaceholderID and ItemPlaceholderID property of ListView control explicitly else you need to(Refer above code).

 

Downloads

Download Source 

Conclusion

ListView control is one of the versatile controls in the existing data bound control set. Since it gives the full freedom to the developers to have a control on the output html it has the capability to replace all the databound controls. Grouping is another frequently used feature in data display and we have learnt how we can use the ListView control to achieve it very easily.

 

Download the source attached with this article to see it in action.

 

Happy Coding!!!

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
group
good..tnq so much
grouping
great article. I tried the above code it worked for me.
thanks a lot
grouping
If I want group based on department means how to do
Comment
hi alex.. nice article and its working.....
grouping
not working properly.....
RE:Who is the right author
Hi Alex,
Thanks for letting us know. I have informed this to dotnetspider. It was copied from Codedigest.
Who is the right author
I came across the code sample http://www.codedigest.com/CodeDigest/153-How-to-Call-a-Method-in-ASPX-Page(CodeBehind)-from-a-UserControl-in-C--and-ASP-Net--.aspx

and I found the same here

http://www.dotnetspider.com/resources/37547-How-Call-Method-ASPX-Page-CodeBehind.aspx

Someone is ripping..Don't know who