简体   繁体   中英

RadListView databinding with entity framework - insert/update/delete

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!

EDIT: If there is a better way to do 2-way data binding using Entity Framework, I'd love to hear those suggestions, as well. I am not set on the current way I've been attempting things and just want to know the best way to do this.

To do so is actually quite easy. Look here for how the Telerik demo handles manual CRUD commands.

Focus on RadListView2_ItemUpdating as it is what you are looking to use. As long as you know the update goes through fine then you can finish there. The problem will be if it fails, causing you to have one set of data in your datasource for your listview and the other set of data in your DB.

Handling this is up to you really, as you could just reload the data from the DB to make sure the user sees what is actually there, or you could alert them to the error to submit a help ticket. You get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM