自定义服务器控件中的DetailsView和EntityDataSource的CRUD操作

时间:2011-03-26 18:54:23

标签: asp.net crud detailsview entitydatasource

我不是ASPX页面的忠实粉丝,拖放等等。我正在构建一个带有单个Default.aspx的门户,其他所有东西都是自定义Web部件控件或服务器控件,其他开发人员可以在已编译的dll中构建,用户可以自己上传到门户以向门户添加功能。我一直在与entitydatasource的DetailsView crud操作作斗争。我做了一个带拖放的test.aspx页面,一切正常,但100%的代码落后,没有什么。未显示错误,但数据未持久保存到数据库。我尝试捕获详细信息视图的onUpdating事件,是的,事件被触发了,我可以枚举提交的数据,但为什么它不会持久存储到数据库?希望有人可以帮忙解决这个问题。

这是我的代码(尝试从代码隐藏创建所有内容并在将所有内容移动到Web部件之前将它们添加到页面上的占位符仅用于测试目的):

public partial class Test : System.Web.UI.Page
{
    private EntityDataSource eds = new EntityDataSource();       
    public DetailsView dtlview = new DetailsView();     

    protected void Page_Load(object sender, EventArgs e)
    {

        //Initialize Datasource              
        eds.ConnectionString = "name=DBEntities";
        eds.DefaultContainerName = "DBEntities";
        eds.EnableDelete = true;
        eds.EnableFlattening = false;
        eds.EnableInsert = true;
        eds.EnableUpdate = true;
        eds.EntitySetName = "EmailAccounts";            
        Controls.Add(eds);//I don't know if this is necessary           

        //Create DetailsView and configure for inserting on default
        dtlview.DataSource = eds;
        dtlview.AutoGenerateInsertButton = true;
        dtlview.AutoGenerateDeleteButton = true;
        dtlview.AutoGenerateEditButton = true;
        dtlview.AutoGenerateRows = false;
        dtlview.DefaultMode = DetailsViewMode.Insert;
        dtlview.AllowPaging = true;
        dtlview.DataKeyNames = new string[] { "ID" };
        dtlview.AllowPaging = true;

        //Create fields since autogeneraterows is false
        BoundField bfID = new BoundField();
        bfID.DataField = "ID";
        bfID.HeaderText = "ID:";
        BoundField bfUserID = new BoundField();
        bfUserID.DataField = "UserID";
        bfUserID.HeaderText = "User ID:";
        BoundField bfDisplayName = new BoundField();
        bfDisplayName.DataField = "DisplayName";
        bfDisplayName.HeaderText = "Display Name:";
        BoundField bfEmailAddress = new BoundField();
        bfEmailAddress.DataField = "EmailAddress";
        bfEmailAddress.HeaderText = "Email:";
        BoundField bfPassword = new BoundField();
        bfPassword.DataField = "Password";
        bfPassword.HeaderText = "Password:";
        BoundField bfOutgoingServer = new BoundField();
        bfOutgoingServer.DataField = "OutgoingServer";
        bfOutgoingServer.HeaderText = "Outgoing server:";
        BoundField bfIncomingServer = new BoundField();
        bfIncomingServer.DataField = "IncomingServer";
        bfIncomingServer.HeaderText = "Incoming Server:";
        CheckBoxField chkfIsDefault = new CheckBoxField();
        chkfIsDefault.DataField = "IsDefault";
        chkfIsDefault.HeaderText = "Is Default?";

        dtlview.Fields.Add(bfID);
        dtlview.Fields.Add(bfUserID);
        dtlview.Fields.Add(bfDisplayName);
        dtlview.Fields.Add(bfEmailAddress);
        dtlview.Fields.Add(bfPassword);
        dtlview.Fields.Add(bfOutgoingServer);
        dtlview.Fields.Add(bfIncomingServer);
        dtlview.Fields.Add(chkfIsDefault);

        dtlview.DataBind();

        //Events handling for detailsview
        dtlview.ItemInserting += dtlview_ItemInserting;
        dtlview.ItemInserted += dtlview_ItemInserted;
        dtlview.ModeChanging += dtlview_ModeChanging;           

        //Add controls to place holder               
        PlaceHolder2.Controls.Add(dtlview);
    }        
    protected void dtlview_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
       e.Values["UserID"] = GetCurrentUserID();           
    }
    protected void dtlview_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {

    }
    protected void dtlview_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        dtlview.ChangeMode(e.NewMode);
        if (e.NewMode != DetailsViewMode.Insert)
        {
            dtlview.DataSource = eds;
            dtlview.DataBind();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你要做的就是添加:

OnContextCreating = “XXXXDatasource_OnContextCreating”                                           OnContextDisposing = “XXXXDatasource_OnContextDisposing”

到您的EntityDataSource。

然后在你的代码中:

protected void XXXXDatasource_OnContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
{
    e.Context = DBEntities.Entities;
}

protected void XXXXDatasource_OnContextDisposing(object sender, EntityDataSourceContextDisposingEventArgs e)
{
    e.Cancel = true;
}

这样,您的ObjectContext被正确设置为DetailsView使用的EntityDataSource。

至少这是我读作最佳实践(也是每页请求查找一个对象上下文)