是否可以将值传递给中继器的SelectMethod? [英] Is it possible to pass a value to the SelectMethod of a Repeater?

查看:241
本文介绍了是否可以将值传递给中继器的SelectMethod?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.Net 4.5引入了通过SelectMethod属性将数据绑定到如Repeater等控件的新方法:

ASP.Net 4.5 introduces new ways to bind data to controls like the Repeater through the SelectMethod property:

<asp:Repeater runat="server" ItemType="MyData.Reference" 
      SelectMethod="GetReferences">

调用Codebehind方法

calls the Codebehind method

public IEnumerable<Reference> GetReferences()

在嵌套中继器的情况下,是否可以将参数传递给此选择方法不知何故,根据外部中继器的项目获取不同的数据?

In the scenario of nested repeaters, is it possible to pass a parameter to this select method somehow, so that it fetches different data depending on the Item of the outer repeater?

示例:

<asp:Repeater runat="server" ItemType="MyData.Reference" 
        SelectMethod="GetReferences(Item.ID)">

应该调用

public IEnumerable<Reference> GetReferences(int id)

我知道这可以通过ItemDataBound事件来实现,但是我想使用SelectMethod和ItemType简单而清晰的语法。

I know this can be achieved through ItemDataBound events, but I would like to use the much simpler and cleaner syntax of SelectMethod and ItemType.

SelectMethod是否允许参数传递?

Does the SelectMethod allow parameter passing somehow?

如果不,是否有另一种干净的方法从SelectMethod中的外部Repeater Item获取值?

If not, is there another clean way to get the value from the outer Repeater Item within my SelectMethod?

推荐答案

在等待答案的同时,我玩了一下,找到了以下解决方案。

While waiting for answers, I played around a bit and found the following solution.

这可能不是最好的方式,但到目前为止,我没有发现任何问题,它很漂亮straigtforward,所以我只是把它丢在那里。

It might not be the best way to do it, but so far I have found no problems with it and it's pretty straigtforward, so I'll just throw it out there.

<asp:Repeater runat="server" ItemType="MyData.Reference" 
     DataSource='<%# GetReferences(Item.ID) %>'>

本质上我在这里做的是替换 SelectMethod DataSource (Intellisense不会建议,但它仍然工作)。

Essentially what I do here is replace the SelectMethod with DataSource (Intellisense will not suggest it, but it works nonetheless).

这样我可以传递值到GetReferences方法,然后使用返回值进行模型绑定。

This way I can pass a value to the GetReferences method and then uses the return value for model binding.

到目前为止,这是我遇到的最短的解决方案。

So far this is the shortest solution I came across.

这篇关于是否可以将值传递给中继器的SelectMethod?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆