<?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>Powertips &#187; Data Binding</title>
	<atom:link href="http://scripting.chaindb.com/category/dotnet/aspdotnet/data-binding/feed/" rel="self" type="application/rss+xml" />
	<link>http://scripting.chaindb.com</link>
	<description>Programmer tips</description>
	<lastBuildDate>Thu, 22 Dec 2011 02:57:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Eval in ASP.net</title>
		<link>http://scripting.chaindb.com/2010/06/24/eval-in-aspnet/</link>
		<comments>http://scripting.chaindb.com/2010/06/24/eval-in-aspnet/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 14:48:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Bind]]></category>
		<category><![CDATA[Eval]]></category>

		<guid isPermaLink="false">http://scripting.chaindb.com/?p=781</guid>
		<description><![CDATA[Dealing with complex Eval() expressions inside of ItemTemplate or other data containers always makes me do a double take. Here are a few observations and thoughts on how to handle and possibly improve handling of this feature in the future. So, a typical scenario is embedding controls inside of an item template. The template loops [...]]]></description>
			<content:encoded><![CDATA[<p>Dealing with complex Eval() expressions inside of ItemTemplate or other data containers always makes me do a double take. Here are a few observations and thoughts on how to handle and possibly improve handling of this feature in the future.<br />
So, a typical scenario is embedding controls inside of an item template. The template loops through the datasource and you now want to embed values from the datasource into the child controls of the ItemTemplate.</p>
<p >For example:</p>
<p ><span>&lt;</span><span>asp</span><span>:</span><span>repeater</span><span> <span>id</span><span>=&#8221;rptSpecials&#8221;</span> <span>runat</span><span>=&#8221;server&#8221;&gt;</span></span></p>
<p ><span><span> </span><span>&lt;</span><span>itemtemplate</span><span>&gt;</span></span></p>
<p ><span><span> </span><span>&lt;</span><span>asp</span><span>:</span><span>HyperLink</span> <span>runat</span><span>=&#8221;server&#8221;</span></span></p>
<p ><span><span> </span><span>NavigateUrl</span><span>=&#8217;~/item.aspx?sku=</span><span>&lt;%</span># Eval(&#8220;sku&#8221;) <span>%&gt;</span><span>&#8216;</span></span></p>
<p ><span><span> </span><span>Text</span><span>=&#8217;</span><span>&lt;%</span># Eval(&#8220;specialhd&#8221;) <span>%&gt;</span><span>&#8216;/&gt;</span></span></p>
<p ><span><span> </span><span>&lt;/</span><span>itemtemplate</span><span>&gt;</span></span></p>
<p ><span>&lt;/</span><span>asp</span><span>:</span><span>repeater</span><span><span>&gt;</span></span></p>
<p >Quick what does that give you?</p>
<p >Not what you might expect:</p>
<p ><span>&lt;</span><span>a</span><span> <span>href</span><span>=&#8221;item.aspx?sku=&amp;lt;%# Eval(&amp;quot;sku&amp;quot;) %&gt;&#8221;&gt;</span>West Wind Html Help Builder 4.02<span>&lt;/</span><span>a</span><span>&gt;</span></span></p>
<p >It&#8217;s slightly inconsistent isn&#8217;t it? Text expands just fine with the Eval expression, but the NavigateUrl() doesn&#8217;t.</p>
<p >Now can anybody spot why this is happening?</p>
<p >If you change the NavigateUrl expression to:</p>
<p ><span>NavigateUrl</span><span>=&#8217;item.aspx?sku=</span><span>&lt;%</span><span># Eval(&#8220;sku&#8221;) <span>%&gt;</span><span>&#8216;</span></span></p>
<p >it turns out it actually works. The problem is that that ~/ is causing ASP.NET to call ResolveUrl() which takes the whole string and mucks up the string. Take the ~ out and ASP.NET no longer calls ResolveUrl and it actually works.</p>
<p >Now it seems to get this to work anyway is this:</p>
<p ><span>&lt;</span><span>asp</span><span>:</span><span>HyperLink</span><span> <span>runat</span><span>=&#8221;server&#8221;</span></span></p>
<p ><span><span> </span><span>NavigateUrl</span><span>=&#8217;</span><span>&lt;%</span># Request.ApplicationPath <span>%&gt;</span><span>/item.aspx?sku=</span><span>&lt;%</span># Eval(&#8220;sku&#8221;) <span>%&gt;</span><span>&#8216;</span></span></p>
<p ><span><span> </span><span> </span><span>Text</span><span>=&#8217;</span><span>&lt;%</span># Eval(&#8220;specialhd&#8221;) <span>%&gt;</span><span>&#8216;/&gt;</span></span></p>
<p >But that results in the following error:</p>
<p ><strong><span>Compiler Error Message: </span></strong><span>CS1040: Preprocessor directives must appear as the first non-whitespace character on a line</p>
<p></span><strong><span>Source Error:</span></strong><span></span></p>
<table class="MsoNormalTable" border="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td>
<p ><span> </span></p>
</td>
</tr>
<tr>
<td>
<p ><span> </span></p>
<p ><span>Line 7:<span> </span>&lt;tr&gt;</span></p>
<p ><span>Line 8:<span> </span>&lt;td valign=&#8221;top&#8221;&gt;</span></p>
<p ><span>Line 9:<span> </span>&lt;asp:HyperLink runat=&#8221;server&#8221;</span></p>
<p ><span>Line 10:<span> </span>NavigateUrl=&#8217;&lt;%# Request.ApplicationPath %&gt;/item.aspx?sku=&lt;%# Eval(&#8220;sku&#8221;) %&gt;&#8217;</span></p>
<p ><span>Line 11:<span> </span>Text=&#8217;&lt;%# Eval(&#8220;specialhd&#8221;) %&gt;&#8217;/&gt;</span></p>
</td>
</tr>
</tbody>
</table>
<p ><span><br />
</span><strong><span>Source File:</span></strong><span> c:\projects2005\wwstore\UserControls\SpecialsListing.ascx </span><strong><span>Line:</span></strong><span> 9</span></p>
<p >If you add a character (say a&lt;%# …%&gt; then it works). I have no idea why this would be a problem for ASP.NET to parse, all I know is it doesn&#8217;t work.</p>
<p >I suspect there&#8217;s a bug in the ASP.NET parser in this situation, especially for the first case where apparently ASP.NET is calling ResolveUrl before it&#8217;s doing the eval on the embedded string.</p>
<p >With all this sort of headache ultimately it&#8217;s easier to just write out the HREF manually:</p>
<p ><span>&lt;a</span><span> <span>href</span><span>=&#8221;</span><span>&lt;%</span># Request.ApplicationPath <span>%&gt;</span><span>&#8220;</span>/<span>item.aspx</span>?<span>sku</span><span>=</span><span>&lt;%</span># Eval(&#8220;sku&#8221;) <span>%&gt;</span><span>&#8220;&gt;</span></span></p>
<p ><span><span> </span><span>&lt;</span><span>img</span> <span>src</span><span>=&#8221;</span><span>&lt;%</span># Request.ApplicationPath <span>%&gt;</span><span>/itemimages/sm_</span><span>&lt;%</span># Eval(&#8220;Itemimage&#8221;) <span>%&gt;</span><span>&#8220;</span> /&gt;<br />
<span>&lt;/</span><span>a</span><span>&gt;</span></span></p>
<p >I bring this up because this sort of thing seems to happen quite frequently. I really wish there was better support for assigning dynamic values. Maybe something using FormatStrings that could be replaced with some dynamic properties:</p>
<p ><span>NavigateUrl</span><span>=&#8221;~/MyPage/MyPage?Id={0}&#8221;</span><span> <span>Text</span><span>=&#8221;SomeText {1}&#8221;</span></span></p>
<p ><span>Parameter0</span><span>=&#8221;</span><span>&lt;%</span><span># Eval(&#8220;Sku&#8221;) <span>%&gt;</span></span></p>
<p ><span>Parameter1</span><span>=&#8221;</span><span>&lt;%</span><span># Eval(&#8220;Company&#8221;) <span>%&gt;</span></span>&#8220;</p>
<p >It may seem like this is overkill but I find myself running into situations quite frequently where the single string delimiter makes it near impossible to create a clean expression in script code. The above solution would solve that problem in all situations because you&#8217;d eliminate the need to nest the Eval expression with its string delimiter and you get back the use of two string delimiters.</p>
<p >There are workarounds today, they&#8217;re just not quite as declarative. The above behavior could be simulated with something like this:</p>
<p ><span>NavigateUrl</span><span>=&#8217;</span><span>&lt;%</span><span># this.ResolveUrl(&#8220;~/&#8221; + string.Format(&#8220;item.aspx?sku={0}&#8221;,Eval(&#8220;sku&#8221;)) <span>%&gt;</span><span>&#8216;</span></span></p>
<p ><span> </span></p>
<p ><span>Still that doesn&#8217;t really get around the string delimiter issue. Try this:</span></p>
<p ><span>NavigateUrl</span><span>=&#8217;</span><span>&lt;%</span><span># this.ResolveUrl(&#8220;~/&#8221; + string.Format(&#8220;onclick=DoItem(&#8216;{0}&#8217;);&#8221;,Eval(&#8220;sku&#8221;))<span>%&gt;</span><span>&#8216;</span></span></p>
<p >There&#8217;s the problem with the string nesting. Remove the Eval() completely from this expression would fix this problem.</p>
<p >Today my workaround for complex expressions that just don&#8217;t want to work is to create a method on the page or control that returns the value which is workable, but not a solution the average novice will think of.</p>
<p ><span>&lt;</span><span>asp</span><span>:</span><span>HyperLink</span><span> <span>runat</span><span>=&#8221;server&#8221;</span></span></p>
<p ><span><span> </span><span>NavigateUrl</span><span>=&#8217;</span><span>&lt;%</span># this.GetItemUrl( Eval(&#8220;sku&#8221;) as string ) <span>%&gt;</span><span>&#8216;</span></span></p>
<p ><span><span> </span><span>Text</span><span>=&#8217;</span><span>&lt;%</span># Eval(&#8220;specialhd&#8221;) <span>%&gt;</span><span>&#8216;/&gt;</span></span></p>
<p >where GetItemUrl() simply does all of the formatting for the URL.</p>
<p ><a href="http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx">Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://scripting.chaindb.com/2010/06/24/eval-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DataItem in  ASPX</title>
		<link>http://scripting.chaindb.com/2010/06/24/dataitem-in-aspx/</link>
		<comments>http://scripting.chaindb.com/2010/06/24/dataitem-in-aspx/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 14:20:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Databind]]></category>
		<category><![CDATA[DataItem]]></category>

		<guid isPermaLink="false">http://scripting.chaindb.com/?p=778</guid>
		<description><![CDATA[The following examples shows how to use the GridViewRow.DataItem property to retrieve a property of the underlying object to which the GridViewRow is bound when using the LinqDataSource control. I have a GridView whose DataSouce is a LinqDataSource. The LinqDataSource has been set to retrieve all records from the Product entity in the Northwind Linq [...]]]></description>
			<content:encoded><![CDATA[<p>The following examples shows how to use the GridViewRow.DataItem property to retrieve a property of the underlying object to which the GridViewRow is bound when using the LinqDataSource control.</p>
<p>I have a GridView whose DataSouce is a LinqDataSource. The LinqDataSource has been set to retrieve all records from the Product entity in the Northwind Linq to SQL data model like so:</p>
<p>&lt;asp:GridView ID=&#8221;GridView1&#8243; runat=&#8221;server&#8221; AutoGenerateColumns=&#8221;False&#8221; DataSourceID=&#8221;LinqDataSource1&#8243;</p>
<p>OnRowDataBound=&#8221;GridView1_RowDataBound&#8221;&gt;</p>
<p>&lt;Columns&gt;</p>
<p>&lt;asp:BoundField DataField=&#8221;ProductID&#8221; HeaderText=&#8221;ProductID&#8221; ReadOnly=&#8221;True&#8221;&gt;&lt;/asp:BoundField&gt;</p>
<p>&lt;asp:BoundField DataField=&#8221;ProductName&#8221; HeaderText=&#8221;ProductName&#8221; ReadOnly=&#8221;True&#8221;&gt;&lt;/asp:BoundField&gt;</p>
<p>&lt;/Columns&gt;</p>
<p>&lt;/asp:GridView&gt;</p>
<p>&lt;asp:LinqDataSource ID=&#8221;LinqDataSource1&#8243; runat=&#8221;server&#8221; ContextTypeName=&#8221;NorthwindDataContext&#8221;</p>
<p>TableName=&#8221;Products&#8221;&gt;</p>
<p>&lt;/asp:LinqDataSource&gt;</p>
<p>In this case, getting a property of the underlying data object is easy. The LinqDataSource is aware that each object in the result set is of Type Product (as defined in the Linq to SQL class) and creates it accordingly. So all we have to do is cast the DataItem to type Product and then retrieve the productName:</p>
<p>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)</p>
<p>{</p>
<p>if (e.Row.RowType == DataControlRowType.DataRow)</p>
<p>{</p>
<p>Product p = (Product) e.Row.DataItem;</p>
<p>string productName = p.ProductName;</p>
<p>}</p>
<p>}</p>
<p>The LinqDataSource also allows you to shape the data that is retrieved. In the example below,  I have specified a declarative select to retrieve only a subset of the values from the Products entity &#8211; the ProductID and ProductName.</p>
<p>&lt;asp:GridView ID=&#8221;GridView1&#8243; runat=&#8221;server&#8221; AutoGenerateColumns=&#8221;False&#8221; DataSourceID=&#8221;LinqDataSource1&#8243;</p>
<p>OnRowDataBound=&#8221;GridView1_RowDataBound&#8221;&gt;</p>
<p>&lt;Columns&gt;</p>
<p>&lt;asp:BoundField DataField=&#8221;ProductID&#8221; HeaderText=&#8221;ProductID&#8221; ReadOnly=&#8221;True&#8221;&gt;&lt;/asp:BoundField&gt;</p>
<p>&lt;asp:BoundField DataField=&#8221;ProductName&#8221; HeaderText=&#8221;ProductName&#8221; ReadOnly=&#8221;True&#8221;&gt;&lt;/asp:BoundField&gt;</p>
<p>&lt;/Columns&gt;</p>
<p>&lt;/asp:GridView&gt;</p>
<p>&lt;asp:LinqDataSource ID=&#8221;LinqDataSource1&#8243; runat=&#8221;server&#8221; ContextTypeName=&#8221;NorthwindDataContext&#8221;</p>
<p>Select=&#8221;new (ProductID, ProductName)&#8221; TableName=&#8221;Products&#8221;&gt;</p>
<p>&lt;/asp:LinqDataSource&gt;</p>
<p>Since we have defined a custom shape for our result, the LinqDataSouce will create an anonymous type for each object in the resultset like so:</p>
<p>x = {ProductID=17, ProductName=Alice Mutton}</p>
<p>Since we do not know what type to cast the DataItem to, we can use Reflection to retrieve the property. The DataBinder.Eval method already does this so we can retrieve the productName like so:</p>
<p>protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)</p>
<p>{</p>
<p>if (e.Row.RowType == DataControlRowType.DataRow)</p>
<p>{</p>
<p>var x = e.Row.DataItem;</p>
<p>string productName = (string) DataBinder.Eval(e.Row.DataItem, &#8220;ProductName&#8221;);</p>
<p>}</p>
<p>}</p>
<p><a href="http://weblogs.asp.net/rajbk/archive/2007/09/18/linqdatasouce-dataitem-in-code-behind.aspx">Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://scripting.chaindb.com/2010/06/24/dataitem-in-aspx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

