views:

39

answers:

1

Hi

can i save the returning value of eval function in a variable and use it wherever i want? i can call it just in <asp: .... tags. i can't use them in vb methods. is it possible?

A: 

Eval("foo") is a shortcut for DataBinder.Eval(Container.DataItem, "foo"). Container.DataItem contains the actual data you're trying to get at, and the Eval() method simply uses reflection or other means to allow late binding on names.

If you want better compile-time type checking, you can always cast the Container.DataItem to whatever type you want.

You can also re-use DataBinder.Eval() elsewhere, as a shortcut for using Reflection.

Some examples of what you can do are below:

<%# DoSomethingWithData (DataBinder.Eval(Container.DataItem, "Foo")) %>

Sub DoSomethingWithData (Object data)
   SomeProperty = data
   DoSomethingWithData = data
End Sub

<%# DoSomethingWithDataStronglyTyped (Container.DataItem) %>

Sub DoSomethingWithDataStronglyTyped (Object data)
   Dim StronglyTyped as SomeType
   StronglyTyped = DirectCast (data, SomeType)
   DoSomethingWithDataStronglyTyped = DoSomethingElse(StronglyTyped)
End Sub

Sub ExampleOfDataBinderEvalReuse ()

   Dim StronglyTyped as SomeType
   StronglyTyped = GetSomeData()

   ' get "Foo" property of the SomeType class
   DataBinder.Eval(StronglyTyped, "Foo")
End Sub
Justin Grant