在ASP.NET中的GridView的ObjectDataSource控件的删除和插入的方法问题 [英] Problems with Delete and Insert methods of a GridView's ObjectDataSource in ASP.NET
问题描述
我想用一个ObjectDataSource在ASP.NET的GridView。显示在GridView作品中的数据。现在我一个CommandField添加到GridView也使编辑数据。 update方法工作正常,但我有删除和插入的问题:
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:
- 当我点击GridView中删除链接,配置DeleteMethod被调用,但是用错的 testSystemEndpoint 的参数。相反,应删除的业务对象,它与各个领域是'空'裸实例。因此,配置DeleteMethod不能删除的条目。
- 当我点击GridView中没有插入链接后会发生。配置的InsertMethod不叫。
- 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.
我的ObjectDataSource是这样的:
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>
我的经理BusinessObject的是TSRepository.cs:
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,我花了相当多的时间在我自己的code追查一个非常类似的问题。
For question 1, I spent the quite a bit of time tracking down a very similar problem in my own code.
对于我来说,解决办法是在GridView的DataKeyNames属性设置为我的对象的主键列名。一旦我这样做,一切都非常完美。
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.
这篇关于在ASP.NET中的GridView的ObjectDataSource控件的删除和插入的方法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!