Question

I'm having an hard time trying to bind an attribute within a DetailsView EditTemplateField. Here goes my data structure:

Class1{
public int idClass { get; set; }
public Class2 classObject { get; set; }

}

Class2 {
public int idClass2 { get; set;}
}

The Class1 is the ObjectDataSource DataObjectTypeName associated to the dropdownlist. I'm trying to bind a Dropdownlist Value to the idClass2 like this:

Bind("classObject.idClass2");
Was it helpful?

Solution

You can't. AFAIK you can only use Eval (one way binding) with nested objects. Anyway what you can do is to modify the first class with a new property:

Class1{
  public int idClass { get; set; }
  public Class2 classObject { get; set; }

  public int idClass2 { 
    get { return classObject.idClass2; }
    set { classObject.idClass2 = value; }
  }
}

and bind it: Bind(idClass2)

OTHER TIPS

Is it absolutely necessary to have the idClass2 in a separate class? if this is the only property in Class2, why dont you just add the property to the Class1?

Even though you

Otherwise I think you need to extract the object Class2 and then bind it like this

var _classObject = class1.classObject;
Bind("_classObject.idClass2");

You can make a "plain" object

Class1{
    public Class2 class2 { get; set; }
    public int Var { get; set; }
    public int InnerVar { get { return class2.Var; } }
}

Class2{
    public int Var { get; set; }
}

Then you can use the Class1 InnerVar to access to Var in Class2.

when you make an object of classObject you should also make objects of all the classes that are lying in it. in this way you can access classObject.idClass2 , if you dont make object of idClass2 you'll get Null Exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top