开发者

How to get to a DataBound objects methods declaritively inside a ListView?

开发者 https://www.devze.com 2023-02-01 14:12 出处:网络
So I\'m using a ListView and I really want to use declaritive markup to crank out da开发者_JS百科ta that\'s already defined in helper methods on the object. For example, if I\'ve got a ListView bound

So I'm using a ListView and I really want to use declaritive markup to crank out da开发者_JS百科ta that's already defined in helper methods on the object. For example, if I've got a ListView bound to a list of "Project" objects, and I have a method called "GetFormalProjName" which is defined on that "Project" class, how can I hit that method from inside the ListView's ItemTemplate?

It seems like its easy to hit any child objects of my bound "Project" object, and I can even hit those child objects' methods with too much trouble (see below):

<%# ((EmpCoreView)Eval("EmpCoreView")).GetFullName() %>

In the above case, "EmpCoreView" is a child object of the "Project" object that's the source of this Item of the listView. As you can see, I can get to the methods of the EmpCoreView object ("GetFullName()" is a method on object "EmpCoreView"). But for methods that are on the "Project" object, I can't seem to get the syntax right to get to them. I tried using "this" like below:

<%# ((Project)Eval("this")).GetFormalProjName() %>

But this fails as 'this' refers to the ASPX page. So, if I can get to my data bound objects sub-objects and all their methods, then how do I get the data bound object's methods?

And I know how to do this with code, I just want to do it declaratively and figure there's got to be a way that I'm just missing. Thanks!


I think you can refer to Container.DataItem to refer to the "this" pointer you attempted to do: http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx

Once you cast the root object, you can drill down through properties and methods.


According to MSDN and also Mono source code, the Eval method takes the name of a property. So you can not directly reference the current object. As a workaround, you could add a property that returns the object itself and then you can call methods. Alternatively, you could actually add property accessors for the values you want to display and not call methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消