Is it possible to pass a value to the SelectMethod of a Repeater?

谁说我不能喝 提交于 2019-12-03 05:54:15

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

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) %>'>

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

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.

Here is how you do it:

In your outer repeater, place a hidden field, and name a selectmethod for inner repeater:

<asp:Repeater SelectMethod="GetTopLevelStuff">
   <ItemTemplate>       
       <asp:HiddenField runat="server" ID="ItemId" Value="<%# Item.ID %>"/>
       <asp:Repeater SelectMethod="GetSubItems">
           <ItemTemplate>Template code for sub-items here</ItemTemplate
       </asp:Repeater>
   </ItemTemplate
</asp:Repeater>

Then, here's the not-so-well-documented-magic:

 public IQueryable<SubItem> GetSubItems([Control("ItemId")] int itemId)
    {
        return yourDataStore.GetSubItems(itemId);
    } 

The valueprovider in this case can also take a propertyname, useful when using listboxes to get "SelectedValue".

I found your question, did as you did, then I tried this solution instead which works just as well but is much cleaner and more according to the idea of the concept, it seems.

Take a look at the Exercise 1: Model Binding in ASP.NET Web Forms -> Task 3 – Value Providers in Model Binding tutorial.

It is possible to define a some kind of the Control Select Parameter within the SelectMethod signature.

You can use Value Providers

Example:

public IQueryable<Category> GetCategories([Control]int? minProductsCount)
{
}

This a list of ValueProviders:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!