Saturday, June 26, 2010

Fixing "Random" Default Button Behavior (asp.net)

Way back when I first started developing web applications, I quickly determined that you had to have the default button as the first submit button in the HTML markup.

A recent trouble ticket at work detailed that the default button was behaving erratically. It seemed that it was working only every other time. Yet, the location of the button never changed, so how could that be?

Here is a link of an example where I have recreated the issue.

Broken Default Button

To recreate the broken behavior, set the focus in one of the textboxes or a radiobuton and then press the enter key. The panel should minimize and a second panel should now become visible. click on the button in the upper right corner to re-expand the original panel and repeat the test.

every time both panels are visible, the default button switches to the button in the lower panel.

The fix is quite simple. Using asp.net 2.0 or higher simply add this one line to the Page_Load function: Page.Form.DefaultButton = "Button ID";

Fixed Default Button

Tuesday, June 22, 2010

Dynamic type simplifies returning an anonymous type from a function

Ever since Microsoft added anonymous types to C#, I've tried to use them in the manner that Microsoft themselves described: "Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type."

And, it seemed to me that one of the best uses for one of these "temporary" objects is as a return type from a function. Yet, Microsoft expressly designed anonymous types to not be usable as a return type. Sure, you could have the return type as Object, but then you could not cast it back as the type because it is "anonymous".

Here is a Blog Post the explains how to return an anonymous type using a casting function.

Now though it is much simpler. All you need to do is set the return value to "dynamic". Since the dynamic type is determined at run time instead of compile time, compiler type checking is bypassed. At run time, when dynamic variables are referenced, they then have their methods and properties verified. So you can, in code, reference a property without having the compiler knowing if the type actually has the property.

One caveat: This will only work within the same assembly. From a design standpoint, this makes sense. Why pass along an anonymous type between assemblies? It just does not make any sense. Inside a single assembly it is local and you are in full control, but between assemblies? You never know who is going to use it. You cannot assume that they would have any idea what is being returned.


Code Snippet



  1. static void Main(string[] args)

  2. {

  3. var d = ReturnAnonamoustype();

  4.  

  5. Console.WriteLine(d.Name);

  6. Console.Read();

  7. }

  8.  

  9. static dynamic ReturnAnonamoustype()

  10. {

  11. return new { Name = "Bob", Age = 50 };

  12. }



Friday, June 18, 2010

Returning a string array to Javascript from Sliverlight

This took a lot of trial and error, but I finally figured out how to return to Javascript a string array. There where many different examples out on the web to call Javascript, passing a string array. But, no examples had Javascript calling a Silverlight function with a string array as the return value.

I first tried the direct approach:

javascript:
function TalkToSilverlight(data) {
var control = document.getElementById("silverlightControl");
var myStringArray = control.Content.Page.ReturningStringArray();
alert('length = ' + myStringArray.length + " : Value of First Element = " + myStringArray[0]);
}


Silverlight C#
public App()
{
HtmlPage.RegisterScriptableObject("Page", this);
//The rest I've elided
}

[ScriptableMember]
public string[] ReturningStringArray()
{
return new [] { "e", "i", "e", "i", "o" };
}


Well to make a long story short, a small little mention of the IList interface on MSDN was the key, all I "needed" to do (as opposed to all the changes I tried) was change the return value from string[] to IList.

Saturday, June 12, 2010

Serendipity strikes again

Can you believe it? After working my free asp.net template example and learning how to take a free web template and integrating it into asp.net, I acquired quite a few skills.

And now the company that I work for has started to work on a new web offering and for the first time decided to use an outside company to do the web UI design. They delivered HTML, CSS and images. So, now I am doing professionally exactly what I did previously for fun.

It is almost as if I was practicing for my current task. If that is not Serendipity, then I do not know what is.