ListView - CRUD Operations with EntityDataSource

Next database reset in 0 hours, 23 minutes, 3 seconds
Products
Name: Chai
Quantity: 10 boxes x 20 bags
Price: $18.00
Units: 39
Discontinued:
 
Name: Chang
Quantity: 24 - 12 oz bottles
Price: $19.00
Units: 17
Discontinued:
 
Name: Aniseed Syrup
Quantity: 12 - 550 ml bottles
Price: $10.00
Units: 13
Discontinued:
 
Name: Chef Anton's Cajun Seasoning
Quantity: 48 - 6 oz jars
Price: $22.00
Units: 53
Discontinued:
 
Name: Chef Anton's Gumbo Mix
Quantity: 36 boxes
Price: $21.35
Units: 0
Discontinued:
 
Name: Grandma's Boysenberry Spread
Quantity: 12 - 8 oz jars
Price: $25.00
Units: 120
Discontinued:
 
Isolate this demo as a stand-alone application
  • About this demo
  • C#
  • VB

This example illustrates how automatic insert, update and delete data operations can be achieved with RadListView and all ASP 3.5 DataSource control types like EntityDataSource.


What you need to do is to set the following properties:
  • DataSourceID property of the RadListView to the ID of the Data Source control
  • EnableDelete, EnableInsert, EnableUpdate properties of the Data Source control to true
  • DataKeyNames property of RadListView by listing the data source table Primary keys
  • Use the two-way ASP.NET binding expressions Bind() for the editable fields in the listview's body

You may also see how to utilize Telerik OpenAccess ORM as a data access layer for our AJAX controls by visiting the OpenAccess ORM live demos here.

  • DefaultCS.aspx
  • DefaultCS.aspx.cs
  • scripts.js
  • styles.css
<%@ Page Language="c#" Inherits="Telerik.ListViewExamplesCSharp.DataEditing.NET35AutomaticDataEditing.DefaultCS"CodeFile="DefaultCS.aspx.cs"  %>

<%@ Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
    <link href="styles.css" rel="stylesheet" />
    <script type="text/javascript" src="scripts.js">
    </script>
</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:RadFormDecorator RenderMode="Lightweight" ID="RadFormDecorator1" runat="server" DecoratedControls="All" EnableRoundedCorners="false" />
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="ListViewPanel1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="ListViewPanel1"></telerik:AjaxUpdatedControl>
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
        </telerik:RadAjaxLoadingPanel>
        <asp:Panel ID="ListViewPanel1" runat="server">
            <telerik:RadListView ID="RadListView1" RenderMode="Lightweight" DataSourceID="EntityDataSource1" runat="server"
                ItemPlaceholderID="ProductItemContainer" DataKeyNames="ProductID" AllowPaging="true">
                <LayoutTemplate>
                    <fieldset id="FieldSet1">
                        <legend>Products</legend>
                        <table class="layoutTable">
                            <tr>
                                <td class="button">
                                    <telerik:RadButton RenderMode="Lightweight" ID="Button1" runat="server" Text="Add new product" OnClick="AddNewButton_Click"
                                        Visible="<%# !Container.IsItemInserted %>" CausesValidation="false"></telerik:RadButton>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Panel ID="ProductItemContainer" runat="server">
                                    </asp:Panel>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <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>
                            </tr>
                        </table>
                    </fieldset>
                </LayoutTemplate>
                <ItemTemplate>
                    <fieldset class="itemFieldset">
                        <table 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>Discontinued:
                                </td>
                                <td class="cellInfo">
                                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("Discontinued") %>'
                                        Enabled="false"></asp:CheckBox>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <telerik:RadButton RenderMode="Lightweight" ID="Button1" runat="server" CommandName="Edit" Text="Edit" CausesValidation="false"></telerik:RadButton>&nbsp;
                                    <telerik:RadButton RenderMode="Lightweight" ID="Button2" runat="server" CommandName="Delete" OnClientClick="ConfirmDelete(this); return false;"
                                        Text="Delete"></telerik:RadButton>
                                </td>
                            </tr>
                        </table>
                    </fieldset>
                </ItemTemplate>
                <EditItemTemplate>
                    <fieldset class="itemFieldset">
                        <table class="itemTable">
                            <tr>
                                <td class="cellLabel">Name:
                                </td>
                                <td class="cellInfo">
                                    <telerik:RadTextBox RenderMode="Lightweight" ID="TextBox1" runat="server" Text='<%# Bind("ProductName") %>' Width="120px"></telerik:RadTextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                                        ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                                <td>Quantity:
                                </td>
                                <td class="cellInfo">
                                    <telerik:RadTextBox RenderMode="Lightweight" ID="TextBox2" runat="server" Text='<%# Bind("QuantityPerUnit") %>' Width="120px"></telerik:RadTextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>Price:
                                </td>
                                <td class="cellInfo">
                                    <telerik:RadNumericTextBox RenderMode="Lightweight" ID="TextBox3" runat="server" Text='<%# Bind("UnitPrice") %>'
                                        Width="80px" MinValue="0" MaxValue="999">
                                    </telerik:RadNumericTextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>Units:
                                </td>
                                <td class="cellInfo">
                                    <telerik:RadNumericTextBox RenderMode="Lightweight" ID="TextBox4" runat="server" Text='<%# Bind("UnitsInStock") %>'
                                        Width="80px" MinValue="0" MaxValue="100">
                                    </telerik:RadNumericTextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>Discontinued:
                                </td>
                                <td class="cellInfo">
                                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Discontinued") %>'></asp:CheckBox>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <telerik:RadButton RenderMode="Lightweight" ID="Button1" runat="server" CommandName="Update" Text="Update"></telerik:RadButton>
                                    <telerik:RadButton RenderMode="Lightweight" ID="Button2" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false"></telerik:RadButton>
                                </td>
                            </tr>
                        </table>
                    </fieldset>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <fieldset class="itemFieldset">
                        <table class="itemTable">
                            <tr>
                                <td class="cellLabel">Name:
                                </td>
                                <td class="cellInfo">
                                    <telerik:RadTextBox RenderMode="Lightweight" ID="TextBox1" runat="server" Width="120px" Text='<%# Bind("ProductName") %>'></telerik:RadTextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                                        ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                                <td>Quantity:
                                </td>
                                <td class="cellInfo">
                                    <telerik:RadTextBox RenderMode="Lightweight" ID="TextBox2" runat="server" Width="120px" Text='<%# Bind("QuantityPerUnit") %>'></telerik:RadTextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>Price:
                                </td>
                                <td class="cellInfo">
                                    <telerik:RadNumericTextBox RenderMode="Lightweight" ID="TextBox3" runat="server" Width="80px" MinValue="0" MaxValue="999" Text='<%# Bind("UnitPrice") %>'>
                                    </telerik:RadNumericTextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>Units:
                                </td>
                                <td class="cellInfo">
                                    <telerik:RadNumericTextBox RenderMode="Lightweight" ID="TextBox4" runat="server" Width="80px" MinValue="0" MaxValue="100" Text='<%# Bind("UnitsInStock") %>'>
                                    </telerik:RadNumericTextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>Discontinued:
                                </td>
                                <td class="cellInfo">
                                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Discontinued") %>'></asp:CheckBox>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <telerik:RadButton RenderMode="Lightweight" ID="Button1" runat="server" CommandName="PerformInsert" Text="Insert"></telerik:RadButton>
                                    <telerik:RadButton RenderMode="Lightweight" ID="Button2" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false"></telerik:RadButton>
                                </td>
                            </tr>
                        </table>
                    </fieldset>
                </InsertItemTemplate>
            </telerik:RadListView>
        </asp:Panel>
        <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=NorthwindReadWriteEntities"
            DefaultContainerName="NorthwindReadWriteEntities" EntitySetName="Products" EnableDelete="True"
            EnableInsert="True" EnableUpdate="True" AutoGenerateWhereClause="True">
        </asp:EntityDataSource>
        <telerik:RadWindowManager RenderMode="Lightweight" ID="RadWindowManager1" runat="server"></telerik:RadWindowManager>
    </div>
    </form>
</body>
</html>

Find Assistance

Help Us Improve

Was this example helpful?