RadListView databinding with entity framework - insert/update/delete

2 posts, 0 answers
  1. Sesame
    Sesame avatar
    1 posts
    Member since:
    Nov 2013

    Posted 22 Nov 2013 Link to this post

    I am trying to create an ASP.NET page to allow basic CRUD operations using Entity Framework. Using Telerik's RadListView, I have the following HTML:
    <telerik:RadListView ID="RadListView1" runat="server" OnNeedDataSource="RadListView1_NeedDataSource">
    <ItemTemplate>
        <table>
            <tr>
                <td>First Name: 
                    <%#Eval("FirstName")%>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" Width="70px"></asp:Button>
                </td>
            </tr>
        </table>
    </ItemTemplate>
    <EditItemTemplate>
        <table>
            <tr>
                <td>First Name:
                    <asp:TextBox ID="txtFirstName" runat="server" Text='<%#Bind("FirstName")%>'></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" Width="70px"></asp:Button>
                </td>
            </tr>
        </table>
    </EditItemTemplate>

    And here is the code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
             RadListView1.DataSource = GetData();
        }
    }
     
    protected void RadListView1_NeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
    {
        RadListView1.DataSource = GetData();
    }
     
    private List<Person> GetData()
    {
        using (BuildingAccessEntities ctx = new BuildingAccessEntities())
        {
            var query = from a in ctx.People
                        orderby a.FirstName
                        select a;
     
            return query.ToList();
        }
    }

    So far, this code properly displays the populated RadListView. When I click the Edit button, the RadListView switches to Edit mode and allows me to Edit the selected record; however, when I click the Update button to save the record, the changes are not saved.

    I have not yet tried to see if inserts and deletes behave this same way.

    I'm sure 2-way binding should be something very simple with the RadListView, but I am new to this and have not been able to find many examples to make this work other than by using data source controls (SqlDataSource, EntityDataSource, etc.).

    Any assistance will be greatly appreciated!


  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1803 posts

    Posted 27 Nov 2013 Link to this post

    Hello Sesame,

    Thank you for contacting us.

    In your scenario, if you are not going to use EntityDataSource control with automatic update/insert/delete functionality (as demonstrated in this online demo) I may suggest you take a look at our help article and online demo for Manual CRUD operations:

    As you will notice, for manual CRUD operations you should handle the server-side ItemCommand event and depending on the e.CommandName, to perform your custom logic.

    If further assistance is needed, please feel free to contact us again.


    Regards,
    Konstantin Dikov
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top