ASP.NET 中 GridView 的 ObjectDataSource 的删除和插入方法的问题
我想在 ASP.NET 中将 ObjectDataSource 与 GridView 一起使用。在 GridView 中显示数据是可行的。现在,我将 CommandField 添加到 GridView 中,也可以编辑数据。更新方法工作正常,但删除和插入时出现问题:
- 当我单击 GridView 中的删除链接时,将调用配置的删除方法,但使用错误的 testSystemEndpoint 参数。它不是应该删除的业务对象,而是一个所有字段均为“空”的裸实例。因此配置的DeleteMethod无法删除该条目。
- 当我单击 GridView 中的“插入”链接时,没有任何反应。未调用配置的InsertMethod。
我的 ObjectDataSource 如下所示:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="[...].TSEndpoint"
DeleteMethod="Remove" InsertMethod="Add"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetTSEndpoints"
TypeName="[...].TSRepository"
UpdateMethod="Update"></asp:ObjectDataSource>
我的 BusinessObject Manager 是 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 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:
- 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.
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于问题 1,我花了相当多的时间在我自己的代码中追踪一个非常相似的问题。
对我来说,解决方案是将 gridview 中的 DataKeyNames 属性设置为我的对象的主键的列名称。一旦我这样做了,一切都很完美。
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:我仍然不知道为什么不调用InsertMethod。但现在这个解决方法对我来说没问题:我只是在 GridView 下创建了一个表单来添加新条目。
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.