Using Asp.net's Bind() method, how do I bind to the object itself, not one of it's properties?
I'm not sure to what exactly you want to bind. The only thing that would make sense to me at the moment is to bind to some UI control, say a DropDown control for instance.
There usually some text properties for the value being displayed and value properties for the actual value to function as identifier. On the Dropdown
- DataTextField
- DataValueField
There you specify DataTextField = "Firstname"
and DataValueField = "Id"
given that you have an object that has properties "Firstname" and "Id".
On lists you can use the Eval
function directly on your ASPX code or you add server-side controls (i.e. Literals, Labels) inside the list templates and implement the ItemDataBound
event (taking the Repeater as example). Here's a good example which illustrates this further.
Hope I was able to help a little ;)
I think what Ryan means here like if you have to an object like this
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
And if you bind Person
object to anywhere in GridView or Repeater to any DataSource you only bind Person and it get a default bind value from one of its properties.
support we have a variable Ryan
from Person
type so i want to get the variable value from calling <%# Eval("Ryan") %>
not <%# Eval("Ryan.FirstName") %>
I tried to put an attribute DefaultBindingProperty for the class but it's not working
[System.ComponentModel.DefaultBindingProperty("FirstName")]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
So does any one knows how to do it properly?
You will find the answer here: http://stackoverflow.com/questions/1714203/get-binded-value-from-class-property-defaultbindingproperty-is-not-working-for-m/1714292#1714292
I ended up working around this by adding a property called SelfReference that simply returns this
. If anyone reads this and has a better solution, I'd like to hear it.
I figured out a way somehow. Actually it is in "http://msdn.microsoft.com/en-us/library/ms752347.aspx"
ListBox ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="true"/>
Note that although we have emphasized that the Path to the value to use is one of the four necessary components of a binding, in the scenarios which you want to bind to an entire object, the value to use would be the same as the binding source object. In those cases, it is applicable to not specify a Path. Consider the following example:
XAML Copy