Widgets October 2007


I dream in code

About the author

Robert Williams is an internet application developer for the Salem Web Network.
E-mail me Send mail
Go Daddy Deal of the Week: 30% off your order at! Offer expires 11/6/12

Recent comments




Code Project Associate Logo


The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RegisterStartUpScript not working

If, like me, you have just spent an hour or so trying to figure out why your simple alert box isn't popping up on page load after registering the script with Page.ClientScript.RegisterStartUpScript; you might find you are missing a form with a runat="server" tag on your page. It doesn't matter where it is, but it does have to be on the page somewhere, in order for the Javascript to be injected into the page. In the end I figured it out the hard way - close examination of pages where it worked vs pages where it didn't work.

Posted by Williarob on Wednesday, October 31, 2007 6:04 AM
Permalink | Comments (7) | Post RSSRSS comment feed

How to Make a Media Element Loop Indefinitely

I was surprised to see that the XAML media element did not have a loop property built into it. Surely having a clip loop is basic functionality? In any case I came up with a JavaScript solution that seemed much simpler to implement than the pure XAML solution offered on the MSDN website

JavaScript Solution

    handleLoad: function(control, userContext, rootElement)
        this.control = control;

        //  Get a reference to your media element (mine is called "MainMovie")         = control.content.findName("MainMovie");
        // Add an Event Listener for the "MediaEnded" Event"MediaEnded", Silverlight.createDelegate(this,this.movieMediaEnded));

    //When the end of the movie is reached, return the movie to the start and play it again
    movieMediaEnded: function(sender, eventArgs)
        sender.Position = "00:00:00";;

See my solution in action at The Daily Prophet Online.

MSDN's Pure XAML Solution

<Page xmlns=""

xmlns:x="" >


<!-- The MediaElement control plays the sound. -->

<MediaElement Name="myMediaElement" >


<EventTrigger RoutedEvent="MediaElement.Loaded">




<!-- The MediaTimeline has a RepeatBehavior="Forever" which makes the media play

over and over indefinitely.-->

<MediaTimeline Source="media\tada.wav" Storyboard.TargetName="myMediaElement"

RepeatBehavior="Forever" />









Posted by Williarob on Tuesday, October 30, 2007 12:27 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Explore Silverlight Showcase XAML & JS

When I first learned about Silverlight I thought it would be compiled into a package rather like Flash does with an SWF file. While it is not impossible to decompile a Flash file and many tools exist to assist you, it does help to ensure that only the most determined individuals can find out how you made that effect and use it on their own site. 

With Silverlight, all you need is something like Firebug and you can easily locate and download the xaml files, and of course a simple View Source reveals where all the JavaScript is. What this means is that if you visit the Silverlight Showcase, find an example that has a player skin you really like, but you are not a designer and couldn't recreate it yourself, or one that does something that you cannot find "how to" examples of anywhere, you can peek under the hood and find out on your own.

All you need to do is this:

  • Open Firebug
  • Look at the rendered HTML
  • Find the object tag with a type of "application/x-silverlight"
  • Look at the "Source" Parameter and then type it into the address bar.

For Example, the Fox movies demo has a source of "XAML/player.xaml" so typing into your browser will bring up the xaml file, that you can download and open in the editor of your choice. That xaml contains links to the external graphics and you can use the same technique to grab them from the site e.g. Download the Javascript files listed in the <HEAD> section, arrange it all into the same folder structure as it was on the server. Finally create a new silverlight project in Blend, replace the page.xaml with the contents of player.xaml and you have an excellent starting point for your own site. 

Silverlight 1.1 sites are a little more complex. Luckily, someone has written an add-in for Lutz Roeder's Reflector that should make it easier.

Obviously you should respect their copyrights and not simply reuse their code and graphics, but my point is that here in the early days of Silverlight, while most of us are floundering around in the dark - very few Silverlight books have been published at the time of writing - the showcase offers us not only inspiration on what can be produced with Silverlight, but gives us everyting we need to see how it was done, so that we as developers can take it to the next level in our own Silverlight applications.

Categories: Silverlight
Posted by Williarob on Friday, October 26, 2007 9:20 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Security Error Running Blend 2 September Preview on Vista

In the welcome screen or on the file menu, click New or Open project. The error appears in an alert box as "Requested registry access is not allowed." While this bug has been reported to Microsoft and they claim to have fixed it for the next release, that doesn't help you if you are getting this error right now. I came up with the following work around on my machine:

Using Process Monitor I was able to find the problem registry key:


Read access was denied. When I went to check the permissions I was informed that I could not view the current permissions, but I could change them, so I made myself the owner and added read permissions to the Everyone user for


As I changed permissions on each key, regedit told me it failed, but if I closed the permissions dialog and reopened it, I could see the new permissions in place and Microsoft Expression Blend was able to open my solutions without error.

Posted by Williarob on Friday, October 26, 2007 9:15 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Move View State to the Server

In his talk "Hidden Gems in ASP.NET 2.0" at Tech Ed 2007 Jeff Prosise showcased a number of really useful but little known features built into ASP.Net 2.0. The one feature that I implemented immediately on all my sites after his talk was his SessionPageStateAdapter class.

This class moves the bulk of the Viewstate text out of the hidden field on the page and into a session variable on the server, and requires remarkably few lines of code. Follow these steps to implement it on your site:

  1. Create  a folder in your application root and name it "App_Browsers"
  2. Add a new XML File to this folder. It doesn't matter what you call it as long as it has a ".browser" extention. I called mine "Default.browser".
  3. Paste in the following code:

      <browser refID="Default">
          <adapter controlType="System.Web.UI.Page" adapterType="SessionPageStateAdapter" />

  4. Create a new class file (in your App_Code dir for a web site or anywhere in a Web Project) and name it "SessionPageStateAdapter"
  5. Paste in the following code:

    using System;
    using System.Web.UI;
    /// <summary>
    /// Summary description for SessionPageStateAdapter
    /// </summary>
    public class SessionPageStateAdapter : System.Web.UI.Adapters.PageAdapter
      public override PageStatePersister GetStatePersister()
        return new SessionPageStatePersister(this.Page);

That's it! It reduced the viewstate field on this page (at the time of writing) from 9440 characters to just 129 characters!


Categories: ASP.Net
Posted by Williarob on Thursday, October 25, 2007 1:15 PM
Permalink | Comments (0) | Post RSSRSS comment feed