2010-09-24 14 views
5

Enviroment Sil ObjectDataSource: Özel bir nesne üzerinde Delete yöntemi ve ObjectDataSource sorunlar yaşıyorum 2.0sorunlar

asp.net çerçeve. Select, Insert ve Update yöntemleri iyi çalışır. İşte

sınıfların kodudur:

public class Car 
{ 
    public string ID {get; set;}//I know this is 3.0+ syntax. 
    public string Description {get; set;}//I know this is 3.0+ syntax. 

    public bool Equals(Car other) 
    { 
     if (ReferenceEquals(null, other)) return false; 
     if (ReferenceEquals(this, other)) return true; 
     return other.ID == ID; 
    } 

    public override bool Equals(object obj) 
    { 
     if (ReferenceEquals(null, obj)) return false; 
     if (ReferenceEquals(this, obj)) return true; 
     return obj.GetType() == typeof (Car) && Equals((Car) obj); 
    } 

    public override int GetHashCode() 
    { 
     return ID; 
    } 
} 

public class CarList 
{ 
    private static List<Car> _cars; 

    public CarList() 
    { 
     if(_cars == null) 
      _cars = new List<Car>(); 

     //Create some cars and insert them here... 
    } 

    public List<Car> Select() 
    { 
     return _cars; 
    } 

    public void Update(Car updatedCar) 
    { 
     int i = _cars.IndexOf(updatedCar); 
     if(i != -1) 
     { 
      _cars[i] = updatedCar; 
     } 
    } 

    public void Insert(Car insertedCar) 
    { 
     _cars.Add(insertedCar); 
    } 

    public void Delete(Car deletedCar) 
    { 
     _cars.Remove(deletedCar); 
    } 
} 

Ve bu ObjectDataSource için kod ve DetailsView: Ben Öğe silmek ve bir hata ayıklama yapmaya çalıştığınızda

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    DataObjectTypeName="Car" 
    TypeName="CarList" 
    DeleteMethod="Delete" 
    InsertMethod="Update" 
    SelectMethod="Select" 
    UpdateMethod="Update"> 
</asp:ObjectDataSource> 

<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" 
    CellPadding="4" DataSourceID="ObjectDataSource1" ForeColor="#333333" GridLines="None" 
    Height="50px" Width="125px"> 
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" /> 
    <RowStyle BackColor="#EFF3FB" /> 
    <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" /> 
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
    <Fields> 
     <asp:BoundField DataField="Id" HeaderText="Id" /> 
     <asp:BoundField DataField="Description" HeaderText="Model" /> 
     <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" /> 
    </Fields> 
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <EditRowStyle BackColor="#2461BF" /> 
    <AlternatingRowStyle BackColor="White" /> 
</asp:DetailsView> 

, J = 0 descript: yöntem Delete, bu gibi parametre deletedCar sahip iyon = boş

Ama Car nesne doğru doğru verilerle doldurulur Update veya Insert Yöntemleri bir derlerim zaman.

Neler oluyor?

cevap

8

DetailsView içinde DataKeyNames'i (Birincil anahtar özellik) ayarlayın.

<asp:DetailsView DataKeyNames="ID" /> 
+0

Evet, öyleydi, teşekkürler! – Unlimited071