In this tutorial, we will look at how we can use the new ListView Control to sort data from a database. We will be using Link Buttons to create the headers ourselves, and add the ability to sort by these.
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
The first thing we need to do is to create a database. We will start by adding a SQL database to our project, and create one table with three columns - id, name and age. Then we will add some sample data to display.
Once we have our database, we will add a ListView Control and a SqlDataSource:
<form id="form1" runat="server"> <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"> </form> </asp:ListView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [tblPeople]"></asp:SqlDataSource> |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
We configure the DataSource with our database, and then assign it to our ListView. Next, we will create the LayoutTemplate:
<LayoutTemplate> <table> </LayoutTemplate> <thead> <tr> <th> </tr><asp:LinkButton ID="lnkName" runat="server" CommandName="Sort" CommandArgument="Name" Text="Name" /> </th><th> <asp:LinkButton ID="lnkAge" runat="server" CommandName="Sort" CommandArgument="Age" Text="Age" /> </th></thead> <tbody> <asp:PlaceHolder ID="itemPlaceholder" runat="server" /> </tbody></table> |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The LayoutTemplate serves as a container for the ItemTemplate. The PlaceHolder will be replaced with the contents of the ItemTemplate. Notice that we pass the CommandName and CommandArgument of each LinkButton.
Next, we will add the ItemTemplate and the EmptyDataTemplate:
<ItemTemplate> <tr> </ItemTemplate><td><%# Eval("Name") %></td> <td><%# Eval("Age") %></td> </tr> <EmptyDataTemplate> Sorry, no data to display. </EmptyDataTemplate> |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
Because the ItemTemplate replaces the PlaceHolder we added in the LayoutTemplate, we simply add the data from the datbase into table cells of a table row. The web application can now be run, and the Link buttons can be clicked to sort the data.
The ASPX page will look something like this:
<form id="form1" runat="server"> <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"> </form> <LayoutTemplate> </asp:ListView><table> </LayoutTemplate><thead> <tr> <th> </tr><asp:LinkButton ID="lnkName" runat="server" CommandName="Sort" CommandArgument="Name" Text="Name" /> </th><th> <asp:LinkButton ID="lnkAge" runat="server" CommandName="Sort" CommandArgument="Age" Text="Age" /> </th></thead> <tbody> <asp:PlaceHolder ID="itemPlaceholder" runat="server" /> </tbody></table> <ItemTemplate> <tr> </ItemTemplate><td><%# Eval("Name") %></td> <td><%# Eval("Age") %></td> </tr> <EmptyDataTemplate> Sorry, no data to display. </EmptyDataTemplate><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [tblPeople]"></asp:SqlDataSource> |
'ASP.NET' 카테고리의 다른 글
ASP.NET에서 Trace를 이용한 웹페이지 속도 및 성능 측정 (0) | 2009.01.29 |
---|---|
jQuery UI로 달력과 날짜입력기(DatePicker)를 붙여보기. (0) | 2009.01.29 |
GET Method 길이 제한 (최대 URL 길이 ) (0) | 2008.12.31 |
새로워진 IIS 7.0으로의 이전 (0) | 2008.12.03 |
ASP.NET AJAX를 사용한 끌어서 놓기 기능의 구현 (0) | 2008.11.27 |