New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Simple vs Advanced Binding

Advanced binding with NeedDataSource event handling

Products
Name: Tofu
Quantity: 40 - 100 g pkgs.
Price: $23.25
Units: 35
Available: Yes


Name: Genen Shouyu
Quantity: 24 - 250 ml bottles
Price: $15.50
Units: 39
Available: Yes


Name: Alice Mutton
Quantity: 20 - 1 kg tins
Price: $39.00
Units: 0
Available: No


Name: Carnarvon Tigers
Quantity: 16 kg pkg.
Price: $62.50
Units: 42
Available: Yes


Name: Teatime Chocolate Biscuits
Quantity: 10 boxes x 12 pieces
Price: $9.20
Units: 25
Available: Yes


Name: Sir Rodney's Marmalade
Quantity: 30 gift boxes
Price: $81.00
Units: 40
Available: Yes



Simple binding

Products
Name: Tofu
Quantity: 40 - 100 g pkgs.
Price: $23.25
Units: 35
Available: Yes


Name: Genen Shouyu
Quantity: 24 - 250 ml bottles
Price: $15.50
Units: 39
Available: Yes


Name: Alice Mutton
Quantity: 20 - 1 kg tins
Price: $39.00
Units: 0
Available: No


Name: Carnarvon Tigers
Quantity: 16 kg pkg.
Price: $62.50
Units: 42
Available: Yes


Name: Teatime Chocolate Biscuits
Quantity: 10 boxes x 12 pieces
Price: $9.20
Units: 25
Available: Yes


Name: Sir Rodney's Marmalade
Quantity: 30 gift boxes
Price: $81.00
Units: 40
Available: Yes


The key to the advanced data binding of Telerik RadListView is handling correctly the NeedDataSource event. That's why we chose this exact name. 'Need' in this case actually means that if at an exact moment the data-source property does not point to a valid data-source object, the listview will not behave correctly.

This event fires in the following cases:
  • Right after OnLoad, Telerik RadListView checks the viewstate for stored listview-related information. If such information is missing (when the page loads for the first time), the NeedDataSource event is fired. This also means that if the EnableViewState property of the control has been set to false, the listview will bind each time the page loads (not only the first time)
  • After automatic sorting
  • When paging event occurs
  • When Select command is fired
  • Right after Edit/Update/Delete/Insert commands finish execution. You can cancel these operations handling the ItemCommand event and assigning false value to the Canceled property of the e event argument
  • When grouping/ungrouping
  • When resorting a group
  • When filtering fields
  • When a call to the Rebind() listview method takes place
  • In some other custom cases.
The advantages of using this event are that the developer does not need to write any code handling the logic about when and how the data-binding should be processed. It is still the developer's responsibility to construct properly a data source object and assign it to the RadListView's DataSource property.
In the code of the NeedDataSource handler you should prepare the data source (list of objects) for Telerik RadListView and assign it to the grid's DataSource property.

Note: You should never call the DataBind() method from inside the NeedDataSource handler or mix simple data-binding mode with advanced data-binding

Alternatively, you can use AccessDataSource/SqlDataSource/ObjectDataSource/XmlDataSource controls to populate Telerik RadListView with data - this is another advanced data-binding mode of the control which allows automatic operations as well. See the the rest of the examples in this section as well as the Insert/Update/Delete section of the online demos for more info.


Simple data-binding through the DataBind() method can be used in simple scenarios which does not require complex operations like insert/delete/update, filtering, etc. For advanced features like data editing, filtering. etc. Telerik RadListView requires advanced data-binding through its NeedDataSource event or a data source control as explained above.

The NeedDataSource event is especially designed to facilitate the work of the developers as you do not need to handle any sorting/paging/filtering/etc. commands manually. Under such circumstances Telerik RadListView will be "smart enough" to perform the corresponding operation automatically. This example illustrates head-to-head comparison between the usage of simple and advanced data binding.

The correct approach when using simple data-binding is to call the DataBind() method on the first page load when !Page.IsPostBack and after handling some event (sort event for example). Keep in mind that if you choose simple data-binding, you will need to assign data-source and rebind the grid after each operation (paging, sorting, editing, etc.) - this copies exactly MS ListView behavior.
  • DefaultCS.aspx
  • DefaultCS.aspx.cs
  • styles.css
<%@ Page Language="c#" Inherits="Telerik.ListViewExamplesCSharp.DataBinding.SimleVsAdvancedBinding.DefaultCS"CodeFile="DefaultCS.aspx.cs"  %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
    <link href="styles.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <div class="demo-container">
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadDataPager1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadDataPager1"></telerik:AjaxUpdatedControl>
                        <telerik:AjaxUpdatedControl ControlID="ListViewPanel1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="RadDataPager2">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadDataPager2"></telerik:AjaxUpdatedControl>
                        <telerik:AjaxUpdatedControl ControlID="ListViewPanel2" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="ddList1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="ListViewPanel1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="ddList2">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="ListViewPanel2" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="ListViewPanel1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="ListViewPanel1" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="ListViewPanel2">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="ListViewPanel2" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" MinDisplayTime="0">
        </telerik:RadAjaxLoadingPanel>
        <telerik:RadFormDecorator RenderMode="Lightweight" ID="RadFormDecorator1" runat="server" DecoratedControls="All" ControlsToSkip="H4H5H6" />
        <h3>Advanced binding with NeedDataSource event handling</h3>
        <asp:Panel ID="ListViewPanel1" runat="server">
            <telerik:RadListView ID="RadListView1" RenderMode="Lightweight" Width="97%" AllowPaging="True" runat="server"
                OnNeedDataSource="RadListView1_NeedDataSource" ItemPlaceholderID="ProductsHolder"
                DataKeyNames="ProductID">
                <LayoutTemplate>
                    <fieldset class="layoutFieldset" id="FieldSet1">
                        <legend>Products</legend>
                        <asp:Panel ID="ProductsHolder" runat="server">
                        </asp:Panel>
                    </fieldset>
                </LayoutTemplate>
                <ItemTemplate>
                    <div class="itemWrapper">
                        <table cellpadding="0" cellspacing="0" class="itemTable">
                            <tr>
                                <td class="cellLabel">Name:
                                </td>
                                <td class="cellInfo">
                                    <%# Eval("ProductName") %>
                                </td>
                            </tr>
                            <tr>
                                <td>Quantity:
                                </td>
                                <td class="cellInfo">
                                    <%# Eval("QuantityPerUnit") %>
                                </td>
                            </tr>
                            <tr>
                                <td>Price:
                                </td>
                                <td class="cellInfo">
                                    <%# DataBinder.Eval(Container.DataItem, "UnitPrice", "{0:C}") %>
                                </td>
                            </tr>
                            <tr>
                                <td>Units:
                                </td>
                                <td class="cellInfo">
                                    <%# Eval("UnitsInStock") %>
                                </td>
                            </tr>
                            <tr>
                                <td>Available:
                                </td>
                                <td class="cellInfo">
                                    <%# ((bool)DataBinder.Eval(Container.DataItem,"Discontinued") == false ? "Yes" : "No") %>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <hr />
                                </td>
                                <td>
                                    <hr />
                                </td>
                            </tr>
                        </table>
                    </div>
                </ItemTemplate>
            </telerik:RadListView>
        </asp:Panel>
        <table cellpadding="0" cellspacing="0" width="100%" class="pagerContainer">
            <tr>
                <td style="width: 76%">
                    <telerik:RadDataPager RenderMode="Lightweight" ID="RadDataPager1" runat="server" PagedControlID="RadListView1"
                        PageSize="6">
                        <Fields>
                            <telerik:RadDataPagerButtonField FieldType="FirstPrev"></telerik:RadDataPagerButtonField>
                            <telerik:RadDataPagerButtonField FieldType="Numeric"></telerik:RadDataPagerButtonField>
                            <telerik:RadDataPagerButtonField FieldType="NextLast"></telerik:RadDataPagerButtonField>
                        </Fields>
                    </telerik:RadDataPager>
                </td>
                <td style="width: 24%;">
                    <asp:Label ID="lblSort1" AssociatedControlID="ddList1" runat="server" Text="Sort by:" CssClass="sortLabel"></asp:Label>
                    <telerik:RadComboBox RenderMode="Lightweight" ID="ddList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddList1_SelectedIndexChanged">
                        <Items>
                            <telerik:RadComboBoxItem Text="-Select field to sort-" Value=""></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Name" Value="ProductName"></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Quantity" Value="QuantityPerUnit"></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Price" Value="UnitPrice"></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Units" Value="UnitsInStock"></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Available" Value="Discontinued"></telerik:RadComboBoxItem>
                        </Items>
                    </telerik:RadComboBox>
                </td>
            </tr>
        </table>
        <br />
        <h3>Simple binding</h3>
        <asp:Panel ID="ListViewPanel2" runat="server">
            <telerik:RadListView ID="RadListView2" RenderMode="Lightweight" Width="97%" AllowPaging="True" runat="server"
                ItemPlaceholderID="ProductsHolder" DataKeyNames="ProductID"
                OnPageIndexChanged="RadListView2_PageIndexChanged">
                <LayoutTemplate>
                    <fieldset class="layoutFieldset" id="FieldSet2">
                        <legend>Products</legend>
                        <asp:Panel ID="ProductsHolder" runat="server">
                        </asp:Panel>
                    </fieldset>
                </LayoutTemplate>
                <ItemTemplate>
                    <div class="itemWrapper">
                        <table cellpadding="0" cellspacing="0" class="itemTable">
                            <tr>
                                <td class="cellLabel">Name:
                                </td>
                                <td class="cellInfo">
                                    <%# Eval("ProductName") %>
                                </td>
                            </tr>
                            <tr>
                                <td>Quantity:
                                </td>
                                <td class="cellInfo">
                                    <%# Eval("QuantityPerUnit") %>
                                </td>
                            </tr>
                            <tr>
                                <td>Price:
                                </td>
                                <td class="cellInfo">
                                    <%# DataBinder.Eval(Container.DataItem, "UnitPrice", "{0:C}") %>
                                </td>
                            </tr>
                            <tr>
                                <td>Units:
                                </td>
                                <td class="cellInfo">
                                    <%# Eval("UnitsInStock") %>
                                </td>
                            </tr>
                            <tr>
                                <td>Available:
                                </td>
                                <td class="cellInfo">
                                    <%# ((bool)DataBinder.Eval(Container.DataItem,"Discontinued") == false ? "Yes" : "No") %>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <hr />
                                </td>
                                <td>
                                    <hr />
                                </td>
                            </tr>
                        </table>
                    </div>
                </ItemTemplate>
            </telerik:RadListView>
        </asp:Panel>
        <table cellpadding="0" cellspacing="0" width="100%" class="pagerContainer">
            <tr>
                <td style="width: 76%;">
                    <telerik:RadDataPager RenderMode="Lightweight" ID="RadDataPager2" runat="server" PagedControlID="RadListView2"
                        PageSize="6">
                        <Fields>
                            <telerik:RadDataPagerButtonField FieldType="FirstPrev"></telerik:RadDataPagerButtonField>
                            <telerik:RadDataPagerButtonField FieldType="Numeric"></telerik:RadDataPagerButtonField>
                            <telerik:RadDataPagerButtonField FieldType="NextLast"></telerik:RadDataPagerButtonField>
                        </Fields>
                    </telerik:RadDataPager>
                </td>
                <td style="width: 24%;">
                    <asp:Label ID="lblSort2" AssociatedControlID="ddList2" runat="server" Text="Sort by:" CssClass="sortLabel"></asp:Label>
                    <telerik:RadComboBox RenderMode="Lightweight" ID="ddList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddList2_SelectedIndexChanged">
                        <Items>
                            <telerik:RadComboBoxItem Text="-Select field to sort-" Value=""></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Name" Value="ProductName"></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Quantity" Value="QuantityPerUnit"></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Price" Value="UnitPrice"></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Units" Value="UnitsInStock"></telerik:RadComboBoxItem>
                            <telerik:RadComboBoxItem Text="Available" Value="Discontinued"></telerik:RadComboBoxItem>
                        </Items>
                    </telerik:RadComboBox>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Support & Learning Resources

Find Assistance