Bug 2397 - Wrong DataItem type in Repeater ItemDataBound event when the data source is a SortedDictionary
Summary: Wrong DataItem type in Repeater ItemDataBound event when the data source is a...
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Web (show other bugs)
Version: 2.10.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-12-07 13:19 UTC by Cédric Belin
Modified: 2012-04-28 13:53 UTC (History)
2 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Cédric Belin 2011-12-07 13:19:43 UTC
I have an ASP.NET site. One page uses a Repeater control.
This control is bound to a SortedDictionary<string, string> collection (returned as a IDictionary<string, string> via the SelectMethod property of a ObjectDataSource control).

I customize the display of the Repeater items in the ItemDataBound event.
Normally, the DataItem property of the RepeaterItemEventArgs should be filled with a KeyValuePair<string, string> instance. Instead, I get a DictionaryEntry instance.

On Windows 7 / IIS 7.5 / .NET Framework 4.0, I get the expected KeyValuePair<string, string> object.
On Mono, if I replace the SortedDictionary<string, string> data source by a Dictionary<string, string>, I get the expected KeyValuePair too.

It seems that there is a bug in SortedDictionary or ObjectDataSource implementation.

My specs : Mac OS X 10.7.2, Mono 2.10.6 (running on Apache and mod_mono) with ASP.NET 4.0 profile.
The bug is reproducible on a Linux Ubuntu 11.04 system with the same settings (Mono version is 2.10.5).


Code extract of the ASPX page :

<asp:ObjectDataSource id="SiteDataSource" TypeName="HomePage.Default" SelectMethod="GetSites" runat="server" />
<asp:Repeater DataSourceID="SiteDataSource" OnItemDataBound="OnSiteDataBound" runat="server">
	<ItemTemplate>
		<li>
			<asp:HyperLink NavigateUrl='<%# Eval("Value") %>' Text='<%# Eval("Key") %>' runat="server" />
			<asp:HyperLink id="AdminLink" ImageUrl="~/Images/Admin.png" ToolTip="Module d'administration" Visible="false" runat="server" />
		</li>
	</ItemTemplate>
</asp:Repeater>


Code extract of the code behind class (the OnSiteDataBound method takes the bug into account) :

public static IDictionary<string, string> GetSites()
{
	var list=new SortedDictionary<string, string>();
	var hiddenDirectories=new List<string>
	{
		Path.GetFileName(HttpContext.Current.Request.PhysicalApplicationPath.TrimEnd('/')),
		Path.GetFileName(WebConfigurationManager.AppSettings["WebTemplates"]),
		"MyDbManager",
		"MyInbox",
		"SvnBrowser"
	};

	foreach(var path in Directory.GetDirectories(WebConfigurationManager.AppSettings["Server.Root"]).Select(x=>Path.GetFileName(x)))
		if(!hiddenDirectories.Contains(path)) list.Add(path, string.Format(CultureInfo.InvariantCulture, "http://dev.{0}/{1}", Global.Host, path));
	
	return list;
}

protected void OnSiteDataBound(object sender, RepeaterItemEventArgs e)
{
	if(e.Item.ItemType!=ListItemType.AlternatingItem && e.Item.ItemType!=ListItemType.Item) return;
	
	string folder, url;
	if(Type.GetType("Mono.Runtime")!=null)
	{
		var monoDataItem=(DictionaryEntry) e.Item.DataItem;
		folder=monoDataItem.Key.ToString();
		url=monoDataItem.Value.ToString();
	}
	else
	{
		var dotnetDataItem=(KeyValuePair<string, string>) e.Item.DataItem;
		folder=dotnetDataItem.Key;
		url=dotnetDataItem.Value;
	}	
	
	if(Directory.Exists(Path.Combine(WebConfigurationManager.AppSettings["Server.Root"], folder, "Admin")))
	{
		var link=(HyperLink) e.Item.FindControl("AdminLink");
		link.NavigateUrl=url+"/Admin";
		link.Visible=true;
	}
}
Comment 1 Dimitar Dobrev 2012-04-28 13:53:33 UTC
I second that, with a small clarification: it has nothing to do with Repeater or with ASP.NET for that matter, it is a bug in SortedDictionary. The problem is in SortedDictionary.Enumerator.IEnumerator.Current - it returns CurrentNode.AsDE () and it should return CurrentNode.AsKV (see https://raw.github.com/mono/mono/master/mcs/class/System/System.Collections.Generic/SortedDictionary.cs).

Note You need to log in before you can comment on or make changes to this bug.