<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MarkvanAalst.com &#187; ListViewDataItem</title>
	<atom:link href="http://www.markvanaalst.com/tag/listviewdataitem/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markvanaalst.com</link>
	<description>Sharing Sitecore tips, tricks and techniques to build better solutions</description>
	<lastBuildDate>Wed, 25 Jan 2012 11:09:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using fieldcontrols in a ListView</title>
		<link>http://www.markvanaalst.com/2008/11/26/using-fieldcontrols-in-a-listview/</link>
		<comments>http://www.markvanaalst.com/2008/11/26/using-fieldcontrols-in-a-listview/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 07:38:45 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Sitecore]]></category>
		<category><![CDATA[DataItem]]></category>
		<category><![CDATA[FieldControl]]></category>
		<category><![CDATA[ItemDatabound]]></category>
		<category><![CDATA[ListView]]></category>
		<category><![CDATA[ListViewDataItem]]></category>

		<guid isPermaLink="false">http://www.markvanaalst.com/?p=12</guid>
		<description><![CDATA[When you want to use a fieldcontrol like a Text, Link or Image control in a ListView you need to specify a datasource. By not specifying a datasource Sitecore looks at the current item. Which in a ListView is not the right one. So you need so set the datasource in code-behind or use an Eval expression in code-before. When you want you use the code-behind option you need to raise an eventhandler which triggers [...]]]></description>
			<content:encoded><![CDATA[<p>When you want to use a fieldcontrol like a Text, Link or Image control in a ListView you need to specify a datasource. By not specifying a datasource Sitecore looks at the current item. Which in a ListView is not the right one. So you need so set the datasource in code-behind or use an Eval expression in code-before.</p>
<p>When you want you use the code-behind option you need to raise an eventhandler which triggers the ItemDataBound handler.In that event you want to get the values of the current item. So you need to cast the ListViewDataItem to an Sitecore item.</p>
<p><span id="more-12"></span></p>
<p><strong>For instance:</strong></p>
<p>Code-before</p>
<p>[sourcecode language="html"]<br />
<asp:ListView ID="ListView1" runat="server"><br />
<layoutTemplate><br />
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder><br />
</layoutTemplate><br />
<itemTemplate></p>
<h1><sc:Text ID="Text1" Field="Title" runat="server" /></h1>
<p><asp:HyperLink ID="BlogPostLink" runat="server">Read more&#8230;</asp:HyperLink></p>
<p></itemTemplate><br />
</asp:ListView><br />
[/sourcecode]</p>
<p>Code-behind<br />
[sourcecode language="csharp"]<br />
ListView1.DataSource = BlogManager.GetAllBlogPosts(Sitecore.Context.Item.ID);<br />
ListView1.ItemDataBound += new EventHandler
<listViewItemEventArgs>(ListView1_ItemDataBound);<br />
ListView1.DataBind();<br />
[/sourcecode]</p>
<p>The datasource of the ListView is a generic list of items (In this case that will be all children matching a specific templateid). Then create an new EventHandler to bind the controls in the ItemTemplate to the current item.</p>
<p>[sourcecode language="csharp"]<br />
void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)<br />
{</p>
<p>Item objEntry = (Item)((ListViewDataItem)e.Item).DataItem;</p>
<p>Text txt = (Text)e.Item.FindControl(&#8220;Text1&#8243;);<br />
txt.DataSource = objEntry.ID.ToString();<br />
}<br />
[/sourcecode]</p>
<p>Instead of the above way I could also use an Eval in the code-before, but then I&#8217;m going to use C# in a way I don&#8217;t want to.<br />
<em>For example:</em><br />
[sourcecode language="html"]</p>
<h1><sc:Text ID="Text1" DataSource='<%#Eval("ID") %>&#8216; Field=&#8221;Title&#8221; runat=&#8221;server&#8221; /></h1>
<p>[/sourcecode]</p>
<p>So why would I use this &#8220;hard&#8221; way, instead I could just create the controls in C#. Well when I am working on a Sitecore project, usually I do all .Net stuff and a frontend developer creates the html and css. So when he wants to edit the html structure it is still possible for him, and there is no need for him to look in the code-behind file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markvanaalst.com/2008/11/26/using-fieldcontrols-in-a-listview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

