Welcome to MSDN Blogs Sign in | Join | Help

News

  • These postings are provided "AS IS" with no warranties, and confer no rights. Use of included code samples are subject to the terms specified Terms of Use
Tip #5 Did you know… How to Databind a ListView control?

ListView control is a new data control that is available with Visual Studio  2008. Using this control you can easily do Insertion, Deletion, Editing, Paging and Sorting of Data. This controls also gives you the flexibility of displaying data in various format by defining user-defined templates.

Here are the steps to databind a ListView to a SQLDataSource:

1. Add a list view control from the Data tab in ToolBox and Visual Studio will generate code as shown in the image below.

AddListViewControl

2. Now let us try to bind this control to a SQLDataSource. For this, let us copy NorthWind.mdf to the App_Data folder in your web site.

3. Configure this SQLDataSource to use “Categories”  table. This will generate the following piece of code:

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
</asp:ListView> 


<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [Categories]">
</asp:SqlDataSource>

4. Bring up the ListView Tasks by clicking on the smart tag.  Click on “Configure ListView” to bring up the “Configure ListView” dialog.

ListView Tasks 

5. The Configure ListView Dialog will look like this. Here, you can select a layout and select one of the predefined styles. In this case, we have used “Grid” and “Professional”. If you want paging to be enabled you can check the “Enable Paging checkbox”  as show in the dialog below. Click OK.

Configure ListView Dialog

6. If you look at the Source View, you can see that Visual studio has inserted a bunch of ListView templates. The two important once are, LayoutTemplate and ItemTemplate. You will also see that there is DataPager control added. This control takes care of paging for you.

 <LayoutTemplate>
    <table id="Table1" runat="server">
    <tr id="Tr1" runat="server">
    <td id="Td1" runat="server">
        <table ID="itemPlaceholderContainer" runat="server" border="1" 
        style="background-color: #FFFFFF;border-collapse: collapse;">
        <tr id="Tr2" runat="server" style="background-color:#DCDCDC;color: #000000;">
        <th id="Th1" runat="server">
        CategoryID</th>
        <th id="Th2" runat="server">
        CategoryName</th>
        <th id="Th3" runat="server">
        Description</th>
        <th id="Th4" runat="server">
        Picture</th>
        </tr>
        <tr ID="itemPlaceholder" runat="server">
        </tr>
        </table>
    </td>
    </tr>
    <tr id="Tr3" runat="server">
    <td id="Td2" runat="server" 
    style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Helvetica,#000000;">
        <asp:DataPager ID="DataPager1" runat="server">
            <Fields>
            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
            ShowNextPageButton="False" ShowPreviousPageButton="False" />
            <asp:NumericPagerField />
            <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
            ShowNextPageButton="False" ShowPreviousPageButton="False" />
            </Fields>
        </asp:DataPager>
    </td>
    </tr>
    </table>
</LayoutTemplate>
<ItemTemplate>
    <tr style="background-color:#DCDCDC;color: #000000;">
    <td>
    <asp:Label ID="CategoryIDLabel" runat="server" 
        Text='<%# Eval("CategoryID") %>' />
    </td>
    <td>
    <asp:Label ID="CategoryNameLabel" runat="server" 
        Text='<%# Eval("CategoryName") %>' />
    </td>
    <td>
    <asp:Label ID="DescriptionLabel" runat="server" 
        Text='<%# Eval("Description") %>' />
    </td>
    <td>
    <asp:Label ID="PictureLabel" runat="server" Text='<%# Eval("Picture") %>' />
    </td>
    </tr>
</ItemTemplate>

7. Run the page and this will display the page with  paging enabled.

Display Data in ListView

Happy coding !

Reshmi Mangalore

SDET, Web Development Tools

Posted: Wednesday, August 20, 2008 7:05 PM by WebDevTools

Comments

Visual Studio Hacks said:

My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. The Web Developer Tools Team announced the release of the Dynamic Data Wizard Preview 0806 for VS 2008 SP1 . US ISV Developer Evangelism

# August 21, 2008 10:36 AM

Visual Web Developer Team Blog said:

Here are three new tips on Tips and Tricks Blog. Tip #4 Did you know… You can add browsers to Visual

# August 29, 2008 7:48 PM

xjb said:

原文地址:HowtoDatabindaListViewcontrolListView是VisualStudio2008中一个新的控件,使用此控件可以很轻松的进行数据的插入、编辑、删除...

# June 11, 2009 9:16 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker