我有来自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; }
}
}
然后我在gridview上使用了List<DHCP.Client>
DataSource
。当我将其中一个数据字段设置为ClientReservation.ReservationActive
时,我收到一个未找到的错误。
我用List&lt;&gt;尝试了这个这根本就没有NULL数据。 所以我有问题:
这可以完成吗?
如果我有一个Client.ClientReservation == null
的对象,我怎么能在DataBind()
上处理它而没有错误?
答案 0 :(得分:0)
问题在于,您可以看到除了ReservationActive
之外,您在构造函数中为所有属性分配了一个值。事实上它仍然是null,这就是你得到错误的原因。所以你应该这样做:
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
而不会收到任何错误。
修改强>
另一个建议。因为您需要添加一些Client
。你可以用这种方式实现这种可能性:
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);
答案 1 :(得分:0)
我找到了解决这两个问题的方法。通过使用这样的模板字段:
<asp:TemplateField>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "ClientReservation.ReservationActive")%>
</ItemTemplate>
</asp:TemplateField>
它得到嵌套类(子属性)的值,如果ClientReservation为NULL,则忽略它而不会出错。