Wednesday, 26 September 2007

First post from Windows Live Writer!

Andre showed me this cute tool. I love it. I got a "Insert Code" plugin as well, now I can do this:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

and this:

protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.DataBind();
}
}

The only thing I am quite annoyed is the white background. I have changed the style to yellow already but no idea why it's still white. If you still see my code has a white background, that means I haven't sort that out yet. Anyway, time to go home for dinner!


P.S. HA, it's yellow now. But I have to get the border!!!

Friday, 21 September 2007

First .NET AJAX page

This is not my first AJAX page, but it's the first one I do for this project. Before this one I haven't really use .NET AJAX much although I have looked into all features and understand most theories about it. So I know a bit more than nothing, but not too much.

I've decided to document this purely for myself because I always forget what I have done, what I have encountered and what I have resolved.

First of all I've made this project into an AJAX enabled application by putting these sections into the web.config file:

1. In configuration\configSections\
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler" type=
"System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type=
"System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
>
<section name="jsonSerialization" type=
"System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type=
"System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
requirePermission="false" allowDefinition="MachineToApplication" />
<section name="authenticationService" type=
"System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
requirePermission="false" allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>

2. In configuration\system.web\
<pages validateRequest="false">
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>

<compilation debug="true">
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>
</compilation>

<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>

3. In configuration\system.web\httpHandlers
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>

4. In configuration\
<system.web.extensions>
<scripting>
<webServices>
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
<!--
<jsonSerialization maxJsonLength="500">
<converters>
<add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>
</converters>
</jsonSerialization>
-->
<!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
<!--
<authenticationService enabled="true" requireSSL = "truefalse"/>
-->

<!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved
and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
writeAccessProperties attributes. -->
<!--
<profileService enabled="true"
readAccessProperties="propertyname1,propertyname2"
writeAccessProperties="propertyname1,propertyname2" />
-->
</webServices>
<!--
<scriptResourceHandler enableCompression="true" enableCaching="true" />
-->
</scripting>

</system.web.extensions>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated" />
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>
</system.webServer>

Then I started to create my control. As everyone may already know, a ScriptManager is required.

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

Next I've added my UpdatePanel. My trigger is a drop down list. When I add the trigger in I thought it would automatically work, but it doesn't! I know this sounds stupid, but, anyway, I forgot to make the drop down list auto post back in the first place. So here is the piece of code I have:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="Div1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="RandomDropDownList" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>

(to be continue... because my days usually pass too quick, I don't have too much time to do development...)

Wednesday, 19 September 2007

Blah 2

We were having lunch in Nando to farewell Alex because Nando is his favourite restaurant.

Andre: I have made Jacqui the co-leader of the user group.
Alex: Oh, so when will you have your talk?
Jacq: When I have something to talk about.
Alex: I guess you already have a lot to talk about.
Jacq: Can I talk about Philosophy in the user group? Like human nature?
Alex: Yeah, I don't think anyone would come though.

Umm, for those who don't know, I am a part time student, doing a Graduated Diploma in Philosophy (University of Auckland). Unfortunately it probably doesn't give me any advantage in work. :D

Any idea for .NET User Group topics?

We are thinking of all possible topics for our .NET User Group. Of cause there are heaps of things we can do, however it's good to gather more idea from the community because real and practical needs can only be found out from people in the community. So if you have any idea, anything you would like us to talk about in the .NET User Group, leave a comment here to let me know. :)

Friday, 14 September 2007

No more "Click to activate and use this control"

Well, I kind of know this trick for ages (really long long time ago). Actually most of the big websites now have changed their code so the annoying "Click to activate and use this control" for objects like flash does not appear anymore. All you have to do, is to have a javascript funcition for creating the object in an external js file. Use the script in your html and call the function to write the object out, like what Sony, Samsung, etc does.

See also : No more "Click to activate and use this control" (2)

Or, like my wedding website (sorry for being budget not having my own domain but rely on blogspot), have external header and footer generation functions, but leave the middle part inside the html. I can't remember why did I do that, but I did.

Anyway a js file with a function like this and call the function in your html will do:

function CreateObject(){
document.write(' <OBJECT id="WeddingHP4.swf");
document.write(' codeBase="...');
document.write(' height="470" width="700" align="middle" classid="..." style="border:solid 2px #FFCECC"');
document.write(' VIEWASTEXT>');
document.write(' <PARAM NAME="Movie" VALUE="WeddingHP4.swf"');
document.write(' <PARAM NAME="Src" VALUE="WeddingHP4.swf">');
document.write(' <embed src="WeddingHP4.swf" quality="high" bgcolor="#ffffff" width="800" height="470" name="WeddingHP4.swf" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="..."> </embed>');
document.write(' </OBJECT>');
}

NAnt task example - XmlPokeExtendedTask

NAnt has a XmlPoke task, which is good, but unfortunately it does not create the node if it does not already exists. I have created my XmlPokeExtendedTask which would create xml node with and the spcified attribute for me.

For example
/configuration/appSettings/add[@key='Some.Setting']/@value,10
/configuration/appSettings/add[@key='Some.Setting2']/@value,Low

the ouput XML will contains the following:
<configuration>
<appsettings>
<add key="Some.Setting" value="10">
<add key="Some.Setting2" value="Low">
</appsettings>
</confugration>


Here is the assembly and an example. Simply putting the dll into the NAnt folder would work because NAnt automatically include assemblies that are named *Tasks.dll.

Wednesday, 12 September 2007

Blah 1

This has nothing to do with development.

I've been reading what I typed this afternoon and have found lots of typo and even sentences that don't make any sense. I was working at the same time I type that up. Have to admit that my multi-tasking skill is not good enough. Anyway, forgive me if you see any typo or spelling or grammatical mistake.

I hope I won't do that in my philosophy essays and exams.

C# 3.0 Basic - Lambda expressions

"A lambda expression is written as a parameter list, followed by the => token, followed by an expression or a statement block."

This is not the first time I see Lambda expressions. I love Lambda expressions in the first sight because the look pretty much like logic to me. Alex talked about this in his LINQ example. I will talk about it in LINQ later, but here are some examples I copy from c#3.0 specification document:

x => x + 1 // Implicitly typed, expression body
x => { return x + 1; } // Implicitly typed, statement body
(int x) => x + 1 // Explicitly typed, expression body
(int x) => { return x + 1; } // Explicitly typed, statement body
(x, y) => x * y // Multiple parameters
() => Console.WriteLine() // No parameters

Honestly I am not confident in typing an example for this in notepad and assume it will work. So i will go home and install VS 2008 beta on my machine first, have a try and create an example. Then I can create example for LINQ as well.:)

C# 3.0 Basic - Anonymous type

"An anonymous object initializer declares an anonymous type and returns an instance of that type."

Cool, another thing in .NET allows anonymous. It's quite handy to have anonymous method and type support.
var meeting = new {Time = 3.Hours().Ago(), Location = "Ellerslie"};

The variable meeting has a type, which, technically not really anonymous. An ugly system name is assigned to this type and which I honestly cannot remember.

If there is another anonymous type with the same initializer, C# 3.0 is smart enough to recognise them as the same type. e.g.
var meeting2 = new {Time = 3.Hours().FromNow(), Location = "CBD"};
meeting = meeting2;

I think this is really cool although I don't have in mind where can I use this yet, haha.

C# 3.0 Basic - Extension methods

Alex's example on extension method as a whole was really cool. I will ask him for it later. Meanwhile I will write a super simplified one to show that I understand what he was saying.:D

First here are two extension methods. The first one, Hours, extends int type; and the second one, FromNow, extends TimeSpan type.
public static TimeSpan Hours(this int i)
{
return new TimeSpan(0, i, 0, 0);
}

public static DateTime FromNow(this TimeSpan t)
{
return DateTime.Now.Add(t);
}

So I can write something like this:
DateTime ThreeHoursFromNow = 3.Hours().FromNow();

Probably my example cannot illustrate the power of extension methods, but well, you get the idea. :)

C# 3.0 Basic - Implicit typing

var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var persons= new Dictionary<int,Order>();

Basically the above are equivalent to
int i = 5;
string s = "Hello";
double d = 1.0;
int[] numbers = new int[] {1, 2, 3};
Dictionary<int,Order> persons= new Dictionary<int,Order>();

"In an implicitly typed local variable declaration, the type of the local variable being declared is inferred from the expression used to initialize the variable". IntelliSense recognises the type of the variable and support normal auto completion of that type for the variable.

Note that the following are example of incorrect declarations:
var x; // Error, no initializer to infer type from
var y = {1, 2, 3}; // Error, collection initializer not permitted
var z = null; // Error, null type not permitted

C# 3.0 Basic - Collection initializer

According to C# 3.0 specification, "a collection initializer consists of a sequence of element initializers, enclosed by { and } tokens and separated by commas".

So
Person p1 = new Person("Jacqualine", "Chow");
Person p2 = new Person("Andre", "Meurer");
Person p3 = new Person("Alex", "James");

List<Person> pList = new List<Person>();
pList.Add(p1);
pList.Add(p2);
pList.Add(p3);

can be written as

List<Person> pList = new List<Person>{ p1, p2, p3};

and in combining the power of object initializer, this will become

List<Person> pList = new List<Person>
{
new Person { Firstname = "Jacqualine", Lastname = "Chow" },
new Person { Firstname = "Andre", Lastname = "Meurer" },
new Person { Firstname = "Alex", Lastname = "James" }
};

C# 3.0 Basic - Object initializer

This is the first thing I remember from Alex's talk.

According to C# 3.0 specification, "an object initializer consists of a sequence of member initializers, enclosed by { and } tokens and separated by commas".

So
Person p = new Person();
p.Firstname = "Jacqualine";
p.Lastname = "Chow";

and

public Person(string firstname, string lastname)
{
this.Firstname = firstname;
this.Lastname = lastname;
}

Person p = new Person("Jacqualine", "Chow");


can be written as
Person p = new Person { Firstname = "Jacqualine", Lastname = "Chow" };

In this case we do not have to create a lot of constructors for different combination of initial parameters.

Post-.NET User Group 1 - VS 2008 and C#3.0

Thanks for everyone that have attended the .NET User Group meeting held in Olympic Software last night. I was very happy because there are more people than we expected and there are pizzas, haha.

I will try to get Darryl Burling's presentation slide up here later when he has finished his VS 2008 .NET User Group tour these two months. It is not very nice if I ask him for it now and people can get it before his talks. :P

I will post the C# 3.0 cool features and tricks that Alex James showed us here in the upcoming posts. Since Alex didn't have any slides and he is too busy now (see Off to sunny Seattle), I have to dig out what he has said from my little brain. Anyway, will post all (alright, most) of them here soon.

I am very happy to see VS2008 having so many improvements on the design side of things. There are not many developers care about whether things look good or not and about usability issues, but most of them do care about functionality. One of the obvious reason is currently it is very hard to make things look nice. I am very interested to learn about XAML (Silverlight and WPF specifically). It will be good if we can hold XAML based user group here in Auckland as well like Nas Khan is doing in Wellington. The user group in Wellington is called ZamDes (yet for me to find out why).

Andre said I should start blogging about Silverlight. I wish I can really do that soon. :)

Tuesday, 4 September 2007

NAnt task example - Update database

NAnt is a .Net build tool (which obviously similar to Ant). We have been using NAnt to create and deploy patches and I have been creating my own NAnt tasks to do the job. Recently I am thinking of enhancing the script that I have written couple of years ago.

Each user defined task has a TaskName, maybe some TaskAttribute too, and override ExecuteTask method. Let me just post an example of user defined task here.

Nant.UpdateDatabase.build.txt (save this as .build)

.NET User Group 1 - VS 2008 and C#3.0

Olympic is hosting the first Ellerslie .NET User Group meeting next Tuesday. This is a public event free of charge for all developers keen on learning new tips & tricks, and keeping up to date with what’s coming up. We will host regular meetings at the Olympic Auckland office every 4-6 weeks.

Our opening night will be a double session by Darryl Burling of Microsoft (on Visual Studio 2008) and Alex James (on C# 3.0)

If you know anyone who might be interested please tell them about it. Remember it’s an open event so everyone is welcome.

Here are the details:

A lap around Visual Studio 2008 & A lap around C# 3.0
Auckland 11/09/2007
Gather at 5:45, starting at 6:00

A lap around Visual Studio 2008
Presented by Darryl Burling
Explore all the new Visual Studio 2008 features, from language enhancements; improved designers; Web and smart-client development tools; to Visual Studio Team System, a suite of software lifecycle management tools poised to transform how you deliver software for Windows Vista, the 2007 Microsoft Office system, and the Web.

A lap around C# 3.0
Presented by Alex James
Explore quickly the new features of C# 3.0, including things like LINQ, Lambda Expressions, Anonymous Types etc.

Catering: Pizza & Drinks
Door Charge: Free
Parking: Free, just park in Olympic Software’s car park.

Venue
Olympic Software
10 Cawley St
Ellerslie Auckland

Map of venue