简体   繁体   English

RadListView与实体框架的数据绑定-插入/更新/删除

[英]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. 我正在尝试创建一个ASP.NET页,以允许使用实体框架进行基本的CRUD操作。 Using Telerik's RadListView, I have the following HTML: 使用Telerik的RadListView,我有以下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. 到目前为止,此代码正确显示了填充的RadListView。 When I click the Edit button, the RadListView switches to Edit mode and allows me to Edit the selected record; 当我单击“编辑”按钮时,RadListView切换到“编辑”模式,并允许我编辑选定的记录。 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.). 我确定使用RadListView进行2向绑定应该非常简单,但是我对此并不陌生,除了使用数据源控件(SqlDataSource,EntityDataSource等)之外,找不到很多示例来实现此功能。 )。

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. 编辑:如果有更好的方法使用Entity Framework进行2路数据绑定,我也很乐意听到这些建议。 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. 在此处查看Telerik演示如何处理手动CRUD命令。

Focus on RadListView2_ItemUpdating as it is what you are looking to use. 重点是RadListView2_ItemUpdating ,这是您要使用的内容。 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. 你明白了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架 - 批量更新/插入/删除缓慢 - Entity Framework - Bulk update/insert/delete slowness 比较和插入/更新/删除实体框架中的子级 - Comparing and insert/update/delete children in Entity Framework 实体框架6:插入或更新 - Entity Framework 6: Insert or Update 完整插入/更新/删除实体框架中的子实体 - Complete Insert/Update/Delete of Child Entities in Entity Framework 实体框架的通用插入或更新 - Generic Insert or Update for Entity Framework 确定插入或更新实体框架 - Decide Insert or Update on Entity Framework 使用存储过程进行插入,更新,删除时,如何仅更新.NET实体框架中的脏字段? - How to update only dirty fields in .NET entity framework when using stored procedures for insert,update,delete? 实体框架更新:存储更新,插入或删除语句影响了意外的行数 - Entity Framework On Update : Store update, insert, or delete statement affected an unexpected number of rows 实体框架:“存储更新,插入或删除语句影响了意外的行数(0)。” - Entity Framework: “Store update, insert, or delete statement affected an unexpected number of rows (0).” 如何查看SQL生成的用于使用实体框架进行更新,删除和插入的语句? - How do I view SQL generated statement for Update, Delete and Insert with Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM