rmlourenco
rmlourenco

Reputation: 3

ASP.NET C# - Data binding on a object attribute that is within another object

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");

Upvotes: 0

Views: 632

Answers (4)

Ali Umair
Ali Umair

Reputation: 1424

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.

Upvotes: 0

io_exception
io_exception

Reputation: 162

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.

Upvotes: 0

Marcianin
Marcianin

Reputation: 461

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");

Upvotes: 0

onof
onof

Reputation: 17367

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)

Upvotes: 1

Related Questions