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">
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?
Example:
<asp:Repeater runat="server" ItemType="MyData.Reference"
SelectMethod="GetReferences(Item.ID)">
should be calling
public IEnumerable<Reference> GetReferences(int id)
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.
Does the SelectMethod allow parameter passing somehow?
If not, is there another clean way to get the value from the outer Repeater Item within my SelectMethod?
Here is how you do it:
In your outer repeater, place a hidden field, and name a selectmethod for inner repeater:
Then, here's the not-so-well-documented-magic:
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.
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.
Essentially what I do here is replace the
SelectMethod
withDataSource
(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.
You can use Value Providers
Example:
This a list of ValueProviders:
CompositeValueProvider. Represents a value provider whose values come from a list of value providers that implements the
IEnumerable
interface.QueryStringValueProvider Represents a value provider for query strings that are contained in a
NameValueCollection
object.RouteDataValueProvider Represents a value provider for route data that is contained in an object that implements the
IDictionary(Of TKey, TValue)
interface.ControlValueProvider Represents a value provider for control values.
CookieValueProvider Represents a value provider for cookie values.
FormValueProvider Represents a value provider for form values.
ProfileValueProvider Represents a value provider for profile values.
UserProfileValueProvider Represents a value provider for user profiles.
ViewStateValueProvider Represents a value provider for view state values.
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.