Problems with Delete and Insert methods of a GridView's ObjectDataSource in ASP.NET

时间秒杀一切 提交于 2020-01-12 10:55:13

问题


I want to use an ObjectDataSource with a GridView in ASP.NET. Displaying the data in the GridView works. Now I add a CommandField to the GridView to also enable editing the data. The Update Method works fine, but I have Problems with deleting and inserting:

  1. When I click the Delete link in the GridView, the configured DeleteMethod is called, but with the wrong testSystemEndpoint parameter. Instead of the business object that should be deleted, it is a bare instance with all fields being 'null'. Therefore the configured DeleteMethod cannot delete the entry.
  2. When I click the Insert link in the GridView nothing happens. The configured InsertMethod is not called.

My ObjectDataSource looks like this:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        DataObjectTypeName="[...].TSEndpoint" 
        DeleteMethod="Remove" InsertMethod="Add" 
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetTSEndpoints" 
        TypeName="[...].TSRepository" 
        UpdateMethod="Update"></asp:ObjectDataSource>

My BusinessObject Manager is TSRepository.cs:

[DataObject]
public class TSRepository : ITSRepository
{
    private ISessionFactory _sessionFactory;
    private Configuration _configuration;

    public TSRepository()
    {
        _configuration = new Configuration();
        _configuration.Configure();
        _configuration.AddAssembly(typeof(TSEndpoint).Assembly);
        _sessionFactory = _configuration.BuildSessionFactory();
    }

    [DataObjectMethod(DataObjectMethodType.Insert)]
    public void Add(TSEndpoint testSystemEndpoint)
    {
        if (testSystemEndpoint != null)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Save(testSystemEndpoint);
                transaction.Commit();
            }
        }
    }

    [DataObjectMethod(DataObjectMethodType.Update)]
    public void Update(TSEndpoint testSystemEndpoint)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        using (ITransaction transaction = session.BeginTransaction())
        {
            session.Update(testSystemEndpoint);
            transaction.Commit();
        }
    }

    [DataObjectMethod(DataObjectMethodType.Delete)]
    public void Remove(TSEndpoint testSystemEndpoint)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        using (ITransaction transaction = session.BeginTransaction())
        {
            session.Delete(testSystemEndpoint);
            transaction.Commit();
        }
    }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public ICollection<TSEndpoint> GetTSEndpoints()
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            var testSystems = session
                .CreateCriteria(typeof(TSEndpoint))                    
                .List<TSEndpoint>();
            return testSystems;
        }
    }
}

I would be very glad if someone could help me with my two problems.


回答1:


For question 1, I spent the quite a bit of time tracking down a very similar problem in my own code.

For me, the solution was to set the DataKeyNames property in the gridview to the column name of the Primary Key for my object. Once I did this, everything worked perfectly.




回答2:


Concerning question 2: I still don't know why the InsertMethod is not called. But this workaround is ok for me now: I simply made a form under the GridView to add a new entry.



来源:https://stackoverflow.com/questions/8300823/problems-with-delete-and-insert-methods-of-a-gridviews-objectdatasource-in-asp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!