<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.dotnetmafia.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Kevin Williams .NET and Stuff : ASP.NET</title><link>http://www.dotnetmafia.com/blogs/kevin/archive/tags/ASP.NET/default.aspx</link><description>Tags: ASP.NET</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>New Technique For Databinding Sitecore Items</title><link>http://www.dotnetmafia.com/blogs/kevin/archive/2010/09/08/new-technique-for-databinding-sitecore-items.aspx</link><pubDate>Thu, 09 Sep 2010 04:03:00 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:3992</guid><dc:creator>Kevin</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;For a while now, I’ve been primarily using Sitecore sublayouts as my rendering-of-choice.  The suite of controls available are hard to resist, and it’s easier for someone that doesn’t know XSLT or Sitecore to modify the sublayouts (they’re just user controls, after all).  Recently, I’ve started data binding SItecore items to ASP.NET Repeater controls in my sublayouts in a new way using LINQ.&lt;/p&gt;&lt;p&gt;For example, let’s say we have a multilist field on the current item and we want to display a list of the linked items with thumbnails and short descriptions.  Here’s how I might now build my Repeater for that:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&amp;lt;asp:Repeater id=“SeeMoreRepeater” runat=“server”&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;ItemTemplate&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;sc:Image runat=“server” Item=“&amp;lt;%# Container.DataItem %&amp;gt;” Field=“Thumbnail” /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div class=“header”&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;sc:Text runat=“server” Item=“&amp;lt;%# Container.DataItem %&amp;gt;” Field=“Title” /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;p&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;sc:FieldRenderer runat=“server” Item=“&amp;lt;%# Container.DataItem %&amp;gt;” Field=“ShortDescription” /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/p&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;&amp;lt;/asp:Repeater&amp;gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Code-behind looks like this:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;MultiListField seeMoreField = Sitecore.Context.Item.Fields[“SeeMore”];&lt;br /&gt;SeeMoreRepeater.DataSource = seeMoreField.TargetIDs.Select( id =&amp;gt; Sitecore.Context.Database.GetItem(id) );&lt;br /&gt;SeeMoreRepeater.DataBind();&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;This seems like a very clean way to build a Repeater control and a clean way to data bind in code-behind.  Additionally, using the &amp;lt;sc:Image&amp;gt;, &amp;lt;sc:Text&amp;gt;, and &amp;lt;sc:FieldRenderer&amp;gt; controls enables the content to be edited when the page is viewed in Sitecore’s page editor.&lt;/p&gt;&lt;p&gt;What do you think?&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=3992" width="1" height="1"&gt;</description><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/Sitecore/default.aspx">Sitecore</category><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Using WebService Data With Silverlight</title><link>http://www.dotnetmafia.com/blogs/kevin/archive/2008/03/23/using-webservice-data-with-silverlight.aspx</link><pubDate>Mon, 24 Mar 2008 04:58:00 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:541</guid><dc:creator>Kevin</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Now, I want to do something a bit more dynamic.&amp;nbsp; How about we call a web service, get some dynamic data back, and render something based on that data?&amp;nbsp; In this post, I will build a simple example in order to clearly illustrate the concepts.&amp;nbsp; But at the end, I will post an example of something a bit more useful to show what can be done!&lt;/p&gt;
&lt;p&gt;First off, let&amp;#39;s define a simple web service.&amp;nbsp; I used VS2008&amp;#39;s New Item wizard and selected &amp;quot;Web Service&amp;quot; from the dialog.&amp;nbsp; Then, I replaced HelloWorld() with my own GetData() method.&amp;nbsp; Here&amp;#39;s what the code behind for MyService.asmx looks like:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;[&lt;span style="COLOR:#2b91af;"&gt;WebService&lt;/span&gt;(Namespace = &lt;span style="COLOR:#a31515;"&gt;&amp;quot;http://tempuri.org/&amp;quot;&lt;/span&gt;)]&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;[&lt;span style="COLOR:#2b91af;"&gt;WebServiceBinding&lt;/span&gt;(ConformsTo = &lt;span style="COLOR:#2b91af;"&gt;WsiProfiles&lt;/span&gt;.BasicProfile1_1)]&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;[&lt;span style="COLOR:#2b91af;"&gt;ToolboxItem&lt;/span&gt;(&lt;span style="COLOR:blue;"&gt;false&lt;/span&gt;)]&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:green;"&gt;// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;[System.Web.Script.Services.&lt;span style="COLOR:#2b91af;"&gt;ScriptService&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;class&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;MyService&lt;/span&gt; : System.Web.Services.&lt;span style="COLOR:#2b91af;"&gt;WebService&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="COLOR:#2b91af;"&gt;WebMethod&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="COLOR:#2b91af;"&gt;ScriptMethod&lt;/span&gt;]&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;string&lt;/span&gt;[] GetData()&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;string&lt;/span&gt;[] data = &lt;span style="COLOR:blue;"&gt;new&lt;/span&gt;[] { &lt;span style="COLOR:#a31515;"&gt;&amp;quot;first data&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#a31515;"&gt;&amp;quot;second data&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#a31515;"&gt;&amp;quot;third data&amp;quot;&lt;/span&gt; };&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;return&lt;/span&gt; data;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Note I also have uncommented the ScriptService attribute on my class and added a ScriptMethod attribute to my method.&amp;nbsp; Calling web services from Javascript is a whole lot easier using ASP.NET AJAX.&amp;nbsp; You&amp;#39;ll need to have the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=ca9d90fa-e8c9-42e3-aa19-08e2c027f5d6&amp;amp;displaylang=en" target="_blank"&gt;ASP.NET AJAX 1.0 Extensions&lt;/a&gt; installed for this to work, but it&amp;#39;s worth it.&amp;nbsp; Oh, I&amp;#39;m also using the new array initialization syntax from C# 3.0 in case you&amp;#39;ve never seen it before.&amp;nbsp; A real world application would probably query a database or something.&lt;/p&gt;
&lt;p&gt;In order to call the web service from Javascript, ASP.NET AJAX requires us to place a ScriptManager on the page with a ServiceReference pointing to the .asmx.&amp;nbsp; Here&amp;#39;s what mine looks like:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;:&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;ScriptManager&lt;/span&gt; &lt;span style="COLOR:red;"&gt;ID&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;ScriptManager1&amp;quot;&lt;/span&gt; &lt;span style="COLOR:red;"&gt;runat&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Services&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;:&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;ServiceReference&lt;/span&gt; &lt;span style="COLOR:red;"&gt;Path&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;~/MyService.asmx&amp;quot;&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Services&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;asp&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;:&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;ScriptManager&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Thankfully, VS2008&amp;#39;s web application project template already set up my web.config to use ASP.NET AJAX.&amp;nbsp; If you are not using VS2008, you may need to follow the directions at &lt;a href="http://asp.net/AJAX/Documentation/Live/ConfiguringASPNETAJAX.aspx" target="_blank"&gt;http://asp.net/AJAX/Documentation/Live/ConfiguringASPNETAJAX.aspx&lt;/a&gt; to ensure your web.config is set up properly.&lt;/p&gt;
&lt;p&gt;At this point, I can call my web service from Javascript and react to whatever data comes back.&amp;nbsp; The question is - what should it do?&amp;nbsp; For this post, I think I&amp;#39;ll just put up a Silverlight TextBlock and rotate through the web service data when the user clicks on the text.&amp;nbsp; Here&amp;#39;s the XAML:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Canvas&lt;/span&gt;&lt;span style="COLOR:red;"&gt; xmlns&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;http://schemas.microsoft.com/client/2007&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; xmlns&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;:&lt;/span&gt;&lt;span style="COLOR:red;"&gt;x&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:#a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;TextBlock&lt;/span&gt;&lt;span style="COLOR:red;"&gt; x&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;:&lt;/span&gt;&lt;span style="COLOR:red;"&gt;Name&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;MyTextBlock&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; FontSize&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; FontFamily&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;Arial&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Canvas.Top&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Canvas.Left&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Loaded&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;MyTextBlockLoaded&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; MouseLeftButtonDown&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;MyTextBlockClick&amp;quot; /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Canvas&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;First, I will retrieve the data from the web service once the text block has loaded by using the Loaded event.&amp;nbsp; Let me show you the Javascript and then I will explain it...&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;var&lt;/span&gt; index;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;var&lt;/span&gt; data;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;function&lt;/span&gt; MyTextBlockLoaded( sender, args )&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MyService.GetData( GetDataSuccess, GetDataFailed );&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;function&lt;/span&gt; GetDataSuccess( result )&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; data = result;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; index = 0;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UpdateTextBlock();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;function&lt;/span&gt; GetDataFailed()&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; alert( &lt;span style="COLOR:#a31515;"&gt;&amp;#39;Call to MyService.GetData() failed!&amp;#39;&lt;/span&gt; );&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;function&lt;/span&gt; UpdateTextBlock()&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;var&lt;/span&gt; textblock = document.getElementById( &lt;span style="COLOR:#a31515;"&gt;&amp;#39;&amp;lt;%= Silverlight1.ControlId %&amp;gt;&amp;#39;&lt;/span&gt; ).content.findName( &lt;span style="COLOR:#a31515;"&gt;&amp;#39;MyTextBlock&amp;#39;&lt;/span&gt; );&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; textblock.Text = data[index];&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;First I declare a variable to hold the current index into the data array and a variable to hold the data itself.&amp;nbsp; The implementation of the MyTextBlockLoaded() event handler is calling the web service using the proxy built by ASP.NET AJAX.&amp;nbsp; Note that because this is an asynchronous call, you have to pass in event handlers for both success and failure - that is where my GetDataSuccess() and GetDataFailed() functions come from.&amp;nbsp; On success, I store the data, set the initial index to 0, and call the function to set the text in the TextBlock.&amp;nbsp; Note I&amp;#39;m using document.getElementById() to find the Silverlight viewer.&amp;nbsp; I&amp;#39;m using some inline server-side script to get the Id of the Silverlight viewer from my control (covered in a previous post) to pass to getElementById().&amp;nbsp; The viewer object has a content property upon which I can call findName() (also covered in a previous blog post) to get a reference to the TextBlock.&lt;/p&gt;
&lt;p&gt;The last thing to do is handle the MouseLeftButtonDown event on the TextBlock so that the text changes when the user clicks on it.&amp;nbsp; Here&amp;#39;s that Javascript function:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;function&lt;/span&gt; MyTextBlockClick( sender, args )&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;if&lt;/span&gt; ( index &amp;gt;= data.length-1 )&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; index = 0;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; index = index + 1;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; UpdateTextBlock();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Here, I increment the index and when the index reaches the end of the list, I reset it to 0 to go back to the beginning.&amp;nbsp; Then I call UpdateTextBlock() to display the new text.&lt;/p&gt;
&lt;p&gt;You can see this example in action at &lt;a href="http://www.killeverything.com/zak/Silverlight1/WebService1.aspx" target="_blank"&gt;http://www.killeverything.com/zak/Silverlight1/WebService1.aspx&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;At the beginning of this post, I promised to show a more useful example.&amp;nbsp; How about a Silverlight app that displays headlines from an RSS feed and lets you click through to the articles?&amp;nbsp; If you&amp;#39;ve been reading my Silverlight posts, there shouldn&amp;#39;t be anything too new going on here.&amp;nbsp; I have just combined several of the concepts I&amp;#39;ve been learning and blogging about into a single Silverlight app.&amp;nbsp; You can see it running here: &lt;a href="http://www.killeverything.com/zak/Silverlight1/WebService2.aspx" target="_blank"&gt;http://www.killeverything.com/zak/Silverlight1/WebService2.aspx&lt;/a&gt; and the code for both the simple example and the RSS headline viewer are attached to this post.&amp;nbsp; If you&amp;#39;re a .NET head, you might want to look at the source for the web service where I used LINQ to XML to make parsing the RSS feed easy as cake.&amp;nbsp; Oh, and I want to point out the clipping I had to add to the TextBlock.&amp;nbsp; Support for MaxHeight and MaxWidth may be coming in Silverlight 2.0, but clipping is how it&amp;#39;s done for now...&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=541" width="1" height="1"&gt;</description><enclosure url="http://www.dotnetmafia.com/blogs/kevin/attachment/541.ashx" length="32579" type="application/x-zip-compressed" /><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/Web+Services/default.aspx">Web Services</category><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category></item><item><title>My Silverlight Control</title><link>http://www.dotnetmafia.com/blogs/kevin/archive/2008/03/23/my-silverlight-control.aspx</link><pubDate>Sun, 23 Mar 2008 17:00:00 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:540</guid><dc:creator>Kevin</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Putting a Silverlight viewer on a web page is a lot of work.&amp;nbsp; You have to import the Silverlight.js file (and make sure it&amp;#39;s deployed to the web server), create a div to contain the viewer, and write some Javascript code to call Silverlight.createObject().&amp;nbsp; To me, this is just begging for an ASP.NET control - so I whipped up one of my own.&lt;/p&gt;
&lt;p&gt;So what do I want my control to do for me?&amp;nbsp; Let&amp;#39;s start at the top with the Silverlight.js file.&amp;nbsp; I want this to be an embedded resource so that I don&amp;#39;t always have to remember to copy it in to a new project.&amp;nbsp; If you&amp;#39;re not familiar with this technique, it&amp;#39;s been around since .NET 2.0 - keep up! :)&amp;nbsp; You just throw the Silverlight.js file into your project and set it&amp;#39;s build action to &amp;quot;Embedded Resource&amp;quot;.&amp;nbsp; Here&amp;#39;s a screenshot:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.killeverything.com/zak/blogimages/MySilverlightControl_A8A6/image.png"&gt;&lt;img style="BORDER-TOP-WIDTH:0px;BORDER-LEFT-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;BORDER-RIGHT-WIDTH:0px;" height="225" alt="image" src="http://www.killeverything.com/zak/blogimages/MySilverlightControl_A8A6/image_thumb.png" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Add it to your project&amp;#39;s AssemblyInfo.cs like this:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:green;"&gt;// Embeded Resources&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;[&lt;span style="COLOR:blue;"&gt;assembly&lt;/span&gt;: System.Web.UI.&lt;span style="COLOR:#2b91af;"&gt;WebResource&lt;/span&gt;(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;knw.Silverlight.Silverlight.js&amp;quot;&lt;/span&gt;, &lt;span style="COLOR:#a31515;"&gt;&amp;quot;application/x-javascript&amp;quot;&lt;/span&gt;)]&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Then my control&amp;#39;s code tells the page where to find it like this:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;Page.ClientScript.RegisterClientScriptInclude(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;Silverlight.js&amp;quot;&lt;/span&gt;, Page.ClientScript.GetWebResourceUrl(&lt;span style="COLOR:blue;"&gt;this&lt;/span&gt;.GetType(), &lt;span style="COLOR:#a31515;"&gt;&amp;quot;knw.Silverlight.Silverlight.js&amp;quot;&lt;/span&gt;));&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://www.codeproject.com/KB/aspnet/MyWebResourceProj.aspx" target="_blank"&gt;Here&lt;/a&gt; is a link to an article on Code Project that explains the technique in a bit more detail.&lt;/p&gt;
&lt;p&gt;Next, I want my control to render the container div for the Silverlight viewer.&amp;nbsp; This is pretty straightforward - I just override Render() and use the HtmlTextWriter that&amp;#39;s passed in.&amp;nbsp; Here&amp;#39;s what it looks like:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;protected&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;override&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;void&lt;/span&gt; Render(&lt;span style="COLOR:#2b91af;"&gt;HtmlTextWriter&lt;/span&gt; writer)&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;base&lt;/span&gt;.Render(writer);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.AddAttribute(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, ID + &lt;span style="COLOR:#a31515;"&gt;&amp;quot;Div&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.RenderBeginTag(&lt;span style="COLOR:#2b91af;"&gt;HtmlTextWriterTag&lt;/span&gt;.Div);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.RenderEndTag();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Note I&amp;#39;m just concatenating &amp;quot;Div&amp;quot; onto the existing ID of my control when building the div&amp;#39;s id, but that should make it unique.&lt;/p&gt;
&lt;p&gt;The only thing that&amp;#39;s left is the call to Silverlight.createObject().&amp;nbsp; There are a few variables in this call that I thought could be exposed as properties of my control.&amp;nbsp; Specifically, the path to the Xaml document, the height and width of the viewer, and the background color.&amp;nbsp; I&amp;#39;m using C# 3.0, so I took advantage of another language feature - automatic properties.&amp;nbsp; Here&amp;#39;s what that code looks like:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;string&lt;/span&gt; Xaml { &lt;span style="COLOR:blue;"&gt;get&lt;/span&gt;; &lt;span style="COLOR:blue;"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; Height { &lt;span style="COLOR:blue;"&gt;get&lt;/span&gt;; &lt;span style="COLOR:blue;"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;int&lt;/span&gt; Width { &lt;span style="COLOR:blue;"&gt;get&lt;/span&gt;; &lt;span style="COLOR:blue;"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;public&lt;/span&gt; &lt;span style="COLOR:#2b91af;"&gt;Color&lt;/span&gt; BackgroundColor { &lt;span style="COLOR:blue;"&gt;get&lt;/span&gt;; &lt;span style="COLOR:blue;"&gt;set&lt;/span&gt;; }&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Lastly, I need to build the actual javascript to create the Silverlight viewer and register it with the page to run as a startup script.&amp;nbsp; Like this:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;string&lt;/span&gt; createObjectScript = &lt;span style="COLOR:blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="COLOR:#a31515;"&gt;&amp;quot;&amp;lt;script&amp;gt;Silverlight.createObject( \&amp;quot;{0}\&amp;quot;, document.getElementById( \&amp;quot;{1}Div\&amp;quot; ), \&amp;quot;{2}Ctrl\&amp;quot;, {{ width:&amp;#39;{3}&amp;#39;, height:&amp;#39;{4}&amp;#39;, inplaceInstallPrompt:false,background:&amp;#39;{5}&amp;#39;, isWindowless:&amp;#39;false&amp;#39;, framerate:&amp;#39;24&amp;#39;, version:&amp;#39;1.0&amp;#39; }},{{ onError:null,onLoad:null }},null );&amp;lt;/script&amp;gt;&amp;quot;&lt;/span&gt;, Xaml, ID, ID, Width, Height, &lt;span style="COLOR:#2b91af;"&gt;ColorTranslator&lt;/span&gt;.ToHtml(BackgroundColor) );&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;Page.ClientScript.RegisterStartupScript( &lt;span style="COLOR:blue;"&gt;this&lt;/span&gt;.GetType(), ID + &lt;span style="COLOR:#a31515;"&gt;&amp;quot;Create&amp;quot;&lt;/span&gt;, createObjectScript );&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;At first glance, that code looks pretty confusing.&amp;nbsp; All it&amp;#39;s actually doing is using string.Format() to build the call to Silverlight.createObject().&amp;nbsp; {0} is the path to the Xaml, {1} is the ID of the control + &amp;quot;Div&amp;quot; (the id of container div), {2} is the ID of the control + &amp;quot;Ctrl&amp;quot; (a unique id for the viewer itself), {3} is the width, {4} is the height, and {5} is the background color (converted from a System.Drawing.Color using the System.Drawing.ColorTranslator class).&amp;nbsp; Oh yeah, and you have to escape curly braces when using string.Format() by doubling them.&lt;/p&gt;
&lt;p&gt;Ok, that was a lot of work - what has it bought me?&amp;nbsp; Well, now when I want to use Silverlight on an .aspx page, all I have to do is add a reference to my DLL, register it at the top of the .aspx page, and put the control on the .aspx page somewhere.&amp;nbsp; Which looks like this:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;knw&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;:&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Silverlight&lt;/span&gt; &lt;span style="COLOR:red;"&gt;id&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;Silverlight1&amp;quot;&lt;/span&gt; &lt;span style="COLOR:red;"&gt;runat&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="COLOR:red;"&gt;Xaml&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;MySilverlight.xaml&amp;quot;&lt;/span&gt; &lt;span style="COLOR:red;"&gt;BackgroundColor&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;Gray&amp;quot;&lt;/span&gt; &lt;span style="COLOR:red;"&gt;Height&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;200&amp;quot;&lt;/span&gt; &lt;span style="COLOR:red;"&gt;Width&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;200&amp;quot;&lt;/span&gt; &lt;span style="COLOR:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;I will attach the code for both the control and an example of using it to this post.&amp;nbsp; I&amp;#39;ll be using this control in future Silverlight posts here - plus, I do intend to extend it to do more in the future (as I learn more about Silverlight).&amp;nbsp; So, I&amp;#39;ll try to post updates here from time to time.&amp;nbsp; I hope it&amp;#39;s useful to someone! :)&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=540" width="1" height="1"&gt;</description><enclosure url="http://www.dotnetmafia.com/blogs/kevin/attachment/540.ashx" length="13373" type="application/x-zip-compressed" /><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item></channel></rss>