Bind to the object itself, not to one of its properties

Using the Asp.net Bind () method, how can I bind to the object itself, and not to its properties?

+6
data-binding
source share
5 answers

I ended up working on this by adding a SelfReference property that simply returns this . If someone is reading this and has a better solution, I would like to hear it.

+2
source share

I think Ryan means here as if you had such an object

 public class Person { public string FirstName { get; set; } public string LastName { get; set; } } 

And if you bind a Person object anywhere in the GridView or Repeater to any DataSource, you only bind Person and get the default binding value from one of its properties. support we have the Ryan variable from Person , so I want to get the value of the variable from the call <%# Eval("Ryan") %> not <%# Eval("Ryan.FirstName") %>

I tried to put the DefaultBindingProperty attribute for the class, but it does not work

 [System.ComponentModel.DefaultBindingProperty("FirstName")] public class Person { public string FirstName { get; set; } public string LastName { get; set; } } 

Does anyone know how to do this correctly?

+2
source share

I'm not sure what exactly you want to tie. The only thing that makes sense to me at the moment is to associate with some user interface control, for example, for example, the DropDown control.

Typically, some textual properties of a displayed value and value properties for a real value function as an identifier. Drop down list

  • DataTextField
  • DataValueField

There you specify DataTextField = "Firstname" and DataValueField = "Id" if you have an object with the properties "Name" and "Id".

In lists, you can use the Eval function directly in your ASPX code, or add server-side controls (e.g., literals, shortcuts) inside list templates and implement the ItemDataBound event (e.g., Repeater example). Here is a good example that illustrates this further.

Hope I could help a bit;)

+1
source share

Instead, you can use Container.DataItem: Item='<%# Container.DataItem %>'

+1
source share

I somehow understood the way. It is actually located at http://msdn.microsoft.com/en-us/library/ms752347.aspx "

 ListBox ItemsSource="{**Binding**}" IsSynchronizedWithCurrentItem="true"/> 

Note that although we emphasized that the Path to the value used is one of the four necessary components of the binding, in the scripts that you want to bind to the entire object, the value that will be used will be the same as the object of the binding source. In these cases, it is not applicable so as not to indicate the Path. Consider the following example:

XAML Copy

0
source share

All Articles