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.