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

CRUD Operations with EntityDataSource

Next database reset in 1 hours, 29 minutes, 54 seconds
Products
Name: test
Quantity: 1
Price: $0.00
Units: 34
Discontinued:
 
Name: Test
Quantity: 5
Price: $0.00
Units: 5
Discontinued:
 
Name: stss
Quantity: 3
Price: $0.00
Units: 33
Discontinued:
 
Name: 900
Quantity:
Price: $150.00
Units: 10
Discontinued:
 
Name: aa
Quantity:
Price: $11.00
Units: 11
Discontinued:
 
Name: wowh
Quantity:
Price: $334.00
Units: 45
Discontinued:
 
123

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>
<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>

Support & Learning Resources

Find Assistance