This is a bug. Nice catch.
It works if you have a strongly typed ViewData or if your ViewData is an anonymous type.
I was able to do <%=ViewData["Count"]%> when I used List1 as the ViewDataKey and I used an anonymous type for my view data.
It uses a TypeDescriptor to get the properties off the view data and when you are using a regular dictionary the keys are not properties so it throws.
I personally don't know if I like this anyway, its weird if you pass in an IEnumerable because you do not get direct access to the collection, only its properties. So if do this
<uc1:ListPeopleControl runat="server" DataViewKey="People" />
<% foreach(Person person in this.ViewData) { %> <%=person.Name%> <% } %>
The above does not work as ViewData cannot be cast to my IEnumerable<Person> so to make this work I need a container for my collection:
new {List1=new {RealList=new List<Person>()}}.
Now I can pass my ViewDataKey "List1" and access it via the ViewData property ViewData["RealList"]
Am I am missing something?
BTW -- Bug fix is below
Change the following line in in EnsureViewData of ViewUserControl
else
{
_viewData = DataBinder.Eval(viewDataContainer.ViewData, ViewDataKey);
}to
else
{
IDictionary dictionary = viewDataContainer.ViewData as IDictionary;
if (dictionary != null)
{
_viewData = dictionary[ViewDataKey];
}
else
{
_viewData = DataBinder.Eval(viewDataContainer.ViewData, ViewDataKey);
}
}