0

我有一个来自 DHCP 服务器的配置数据,我将其放入如下类中:

public class DHCP
{
    public DHCP()
    {
        this.Scopes = new List<Scope>();
    }

    public List<Scope> Scopes;

    public class Scope
    {
        public Scope(string ScopeAddress, string SubnetMask, string State, string ScopeName, string Comment)
        {
            this.ScopeAddress = ScopeAddress;
            this.SubnetMask = SubnetMask;
            this.State = State;
            this.ScopeName = ScopeName;
            this.Comment = Comment;
        }

        public Scope(string ScopeAddress, string SubnetMask, string State, string ScopeName, string Comment, bool initClients)
        {
            this.ScopeAddress = ScopeAddress;
            this.SubnetMask = SubnetMask;
            this.State = State;
            this.ScopeName = ScopeName;
            this.Comment = Comment;

            if (initClients)
                this.Clients = new List<Client>();
        }

        public void InitClients()
        {
            this.Clients = new List<Client>();
        }

        public void InitReservations()
        {
            this.Reservations = new List<Reservation>();
        }

        public string ScopeAddress { get; set; }
        public string SubnetMask { get; set; }
        public string State { get; set; }
        public string ScopeName { get; set; }
        public string Comment { get; set; }

        public List<Client> Clients;
        public List<Reservation> Reservations;
    }

    public class Client
    {
        public Client(string IPAddress, string SubnetMask, string UniqueID, string LeaseExpires, string ClientType)
        {
            this.IPAddress = IPAddress;
            this.SubnetMask = SubnetMask;
            this.UniqueID = UniqueID;
            this.LeaseExpires = LeaseExpires;
            this.ClientType = ClientType;
        }

        public string IPAddress { get; set; }
        public string SubnetMask { get; set; }
        public string UniqueID { get; set; }
        public string LeaseExpires { get; set; }
        public string ClientType { get; set; }
        public Reservation ClientReservation { get; set; }
    }

    public class Reservation
    {
        public Reservation(string IPAddress, string UniqueID, bool ReservationActive)
        {
            this.IPAddress = IPAddress;
            this.UniqueID = UniqueID;
            this.ReservationActive = ReservationActive;
        }

        public string IPAddress { get; set; }
        public string UniqueID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Type { get; set; }
        public bool ReservationActive { get; set; }
    }
}

然后我有一个List<DHCP.Client>DataSource在gridview上使用的。当我将其中一个数据字段设置为时,ClientReservation.ReservationActive出现未找到错误。

我已经用 List<> 尝试过这个,这根本不是 NULL 数据。所以我不得不提问:

这完全可以做到吗?如果我有一个对象,Client.ClientReservation == null我怎样才能在DataBind()没有错误的情况下处理它?

4

2 回答 2

0

问题是,正如您所看到的,您在构造函数中为所有属性分配了一个值,除了ReservationActive. 事实上它仍然为空,这就是您收到错误的原因。所以你应该这样做:

public class Client
{
    public Client(string IPAddress, string SubnetMask, string UniqueID, string LeaseExpires, string ClientType, Reservation ClientReservation)
    {
        this.IPAddress = IPAddress;
        this.SubnetMask = SubnetMask;
        this.UniqueID = UniqueID;
        this.LeaseExpires = LeaseExpires;
        this.ClientType = ClientType;
        this.ClientReservation = ClientReservation;
    }

    public string IPAddress { get; set; }
    public string SubnetMask { get; set; }
    public string UniqueID { get; set; }
    public string LeaseExpires { get; set; }
    public string ClientType { get; set; }
    public Reservation ClientReservation { get; set; }
}

和一个传递非空值。否则,您可以以这种方式初始化它,而无需通过构造函数传递它:

Client client = new Client();
client.ClientReservation = new Reservation("127.0.0.1", "ID", true); //example

然后您可以使用myClient.ClientReservation而不会出现任何错误。

编辑:

另一个建议。因为你需要添加一些Clients。您可以通过这种方式实现这种可能性:

public Scope
{
    //...
    public void AddClient(string IPAddress, string SubnetMask, string UniqueID, string LeaseExpires, string ClientType, Reservation ClientReservation){
        Clients.Add(new Client(IPAddress, SubnetMask, UniqueID, LeaseExpires, ClientType, ClientReservation));
    }
}

然后:

Scopes = new List<Scope>();
Scope scope = new Scope(/*...*/);
scope.AddClient(new Client(/*...*/)); 
Scopes.Add(scope);
于 2012-11-03T18:19:53.177 回答
0

我找到了解决这两个问题的方法。通过使用这样的模板字段:

<asp:TemplateField>
  <ItemTemplate>
    <%#DataBinder.Eval(Container.DataItem, "ClientReservation.ReservationActive")%>
  </ItemTemplate>
 </asp:TemplateField>

它获取嵌套类(子属性)的值,如果 ClientReservation 为 NULL,则忽略它而不会出错。

于 2012-11-04T09:21:39.370 回答