Sunday, October 25, 2009

Set the DefaultFocus and DefaultButton in a master page

From ASP.NET 2.0 it is possible to set the default focus and default button with the DefaultFocus and DefaultButton properties which HtmlForm has.  DefaultFocus sets which control to get the default focus in a form, and DefaultButton decides which button will fire the click event when users hit the enter key.  In a page which uses a master page, you know the control name will be added a prefix string like ctl00_ContentPlaceHolder1_.

My example form has one EditBox named txtName and one Button named btnView.  If I use the following code in the Page_Load event:

Page.Form.DefaultButton = btnView.ID;
or
Page.Form.DefaultButton = "btnView";

I get the following error:
The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.

The reason is the control name is changed in a master page.  So we have to use the following one in this case:
Page.Form.DefaultButton = btnView.UniqueID;

But for DefaultFocus, UniqueID seems not working for me.  I have to use ClientID instead of UniqueID:
Page.Form.DefaultFocus = txtName.ClientID;

An alternative is use Focus() method.  You could use the following to get the same effect as the above one:
txtName.Focus();

Sometimes you may want 2 or more default buttons on one page.  This can be implemented by putting asp:Panel control around the controls.  In the following example, when the cursor is in TextBox1, Button1 is the default button, otherwise when the cursor is in TextBox2, Button2 is the default button. 

<asp:Panel ID="Panel1" DefaultButton="Button1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Panel>
<br />
<asp:Panel ID="Panel2" DefaultButton="Button2" runat="server">
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
</asp:Panel>

Tuesday, October 13, 2009

Tech Days Toronto 2009

Tech Days Toronto came again. This year it was held one month early, on Sep. 29 and 30, 2009 at Metro Toronto Convention Centre, Downtown Toronto. It's said there were more people (1200 comparing to 1000 last year) attending the conference. It looks like the recession didn't affect companies' budget. :) I like the time and venue of this year. I didn't have to drive in the highway in the rush hour as last year. The time was one month earlier than last year, and Toronto was still warm.

Again this year all the presenters were from the partners of Microsoft, many of which had direct development and project experience. Several of them also presented in last year's Tech days. The good thing is the overall presentation skills of speakers are better than last year. In all 8 sessions (plus one bonus session) I attended, the speakers all had good presentation skills, and the materials were well prepared.

Let's talk about the sessions. In my opinion, this year is better than last year. Especially I like the added Developer Foundations track, which they said is coming from the feedback of many developers. This track was talking about some topics which could bring the developers to another level. One example was the five OO design principles. Of course we cannot expect too much from just 1 hour and 15 minutes (the total time of a session). The thing is you could get some ideas from other developers, and grasp a chance to practice in your own software development.

Except the Developer Foundations track, I mainly took some sessions for developers on Microsoft-based platform, such as Silverlight 3, Expression Blend for Developers and Prism. What I got from the sessions can be summarized as follows:
  1. As Silverlight or WPF developers, we have to use Expression Blend in design time. It's impossible to write complex XAML tags in Visual Studio. Although developers usually don't like the design work, Expression Blend can save you tons of time when you need a complex XAML file, i.e. a multi-dimension animation.
  2. Silverlight 3 adds some new features, one of which is mouse wheel event support. Previously I created my own mouse wheel event handler. I need to check if the new features make the development easier.
  3. I heard several times about Prism and ASP.NET MVC, but I just don't know if they are practical and effective in real business projects. I will spend some time to investigate these frameworks and see if they can improve my projects.
  4. The best practise for developers is to get to know broadly any relative technologies. Some Java techniques could help .NET developers create a good design. Although I had experience in some other fields (except .net and c++) like Java, Mobile development, BI, I need some time to update my knowledge.

Wednesday, January 14, 2009

Using XSLT to display XML files in a rich way

Recently I worked on several projects related with XML and created several XSLT files to display the XML files. In this article I will summarize a variety of ways to render XML files using XSLT.

XSL stands for eXtensible Stylesheet Language, which includes 3 interrelated parts: XSLT, XPath, and XSL-FO. The reason W3C separated XSL to 3 parts is users can re-use some parts with other technologies. For example, XPath can work together with XPointer syntax, while XSLT can be used in Microsoft Office suite to represent data.

XSLT is XSL transformation, a language for transforming XML documents. In the transformation process, XSLT uses XPath to navigate through the XML files. So in order to write XSLT code, you have to know XPath syntax first.

This article will focus on how XSLT can help display XML files in browsers and other applications, and how XSLT can help display the XML files in a rich way.


Open XML files in Browsers directly


When you want to open a XML file in browsers directly, probably you just can view the tree structure of the XML file. However, if you add the following reference in the XML file, the XML file can use the test.xsl file to display itself.
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

You have to create your xslt file test.xsl to let browsers know how to present the xml data.

Open XML files in Microsoft Excel

When you open a XML file which doesn’t have a XSLT file related with in Microsoft Excel, Excel will pop up a dialog to provide 3 options to choose the way to open. In my case, all the 3 options are not good for me. I preferred to open the XML file with a XSLT file. When the above “xml-stylesheet” statement is added to the original XML file, Excel will pop up a dialog to ask if you want to open the XML file with the specified stylesheet. When you click “OK”, Excel can display the XML file in a way similarly with a browser.

You can add more than one “xml-stylesheet” statement in a XML file. When you do this, Excel will ask which XSLT file you will use while opening. You can create several XSLT files, and give users more options. In my case, I created one xslt file which can provide colourful layout, and another one which can separate the data to different tables.

Open XML files on the client

The previous examples use “xml-stylesheet” reference line to tell the applications where the xslt file is. It is applications that do the transformation, that is, browsers and Excel. The disadvantage of this approach is you have to add this reference line to the destination XML file by yourself. Another disadvantage is sometimes the application is non XSLT aware.

You can write some JavaScript code to do the transformation on the client. The following is a very simple test html file which can only be used for Microsoft IE. For other browsers, you can easily find some examples from internet.

<html>
<body>
<script type="text/javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("test.xml")
// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("test.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>

Open XML files on the server

If you have experience working on server to send back HTML to client, you must know the famous Response.Write() function. In order to open XML files on the server, what you need to do is to write back the transformed XML file to client. The following is a simple example:

xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("test.xml"))

xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("test.xsl"))

Response.Write(xml.transformNode(xsl))

ASP.Net has a new class library to do the transformation, but it is very similar to this code.

Add styles to a XSLT file

Since a XSLT file transforms a XML file to a XHTML file, you can use any XHTML style in the XSLT file. The following is an example to display a XML file in a customized table and add color property to cells.
<table style="border: 1px solid #000000; font-family: Tahoma; font-size: 9pt; width: 100%">
<tr style="color:#333333;background-color:#cccccc;">
<td style="background-color:{Book[1]/@color}">
<xsl:value-of select="Book[1]"/>
</td>
</tr>
</table>

Other XSLT techniques

XSLT has a powerful function called document(), which can combine several documents together to display in one page. In my project, I used this function to display a tabbed page which had the paging feature. The layout is like the following:


Then you can switch among 4 views. The 4 views display different content from the same XML document. Within one view, users can navigate through all the pages. Of course if you are a JavaScript guru, you can construct more compelling layout and different paging style.