Tuesday, 30 January 2007

The Perfect Nightmare - John Saul

This is not one of John Saul's best books, but then again, when you say John Saul you say cliche book. All his books contain some sort of drama, usually concerning children, always in a backwater town or suburb, always playing the child in danger, family connection, deep instinctual fears, etc. But some of them are more interesting than others, me liking science related books more, even if I almost always relate to the mad scientists and their evil creations than to the pseudo-moral rednecks that fight against them. This one is plain boring.
The Perfect Nightmare is about a child abductor. He goes for young innocent girls, he takes them into a makeshift playhouse and "plays" with them. Being a mass production book, it presents some brutal violence, almost no sex at all, even if it hints towards it, and the first and largest part of the book is simply puffed with the actions and fears of the people left behind which are mostly average boring people. This makes the book average and boring. The murderer himself is not something truly original, neither is the way he is portrayed.
The ending of the book has a catch, but after reading 90% of it, it doesn't really has any effect. Bottom line: not even for John Saul fans.

Monday, 29 January 2007

Saturday, 27 January 2007

Event Bubbling in ASP.NET 2.0 and templates

I have been working recently with Page.LoadTemplate and ITemplate.InstantiateIn to add content from user controls to pages or other controls. This thing solves the problem of having to declare abstract classes in App_Code, but poses another problem linked to the lack of object references to the user controls you instantiate. What that means, among other things, is that you lose the ability to use events in your code.

But there is a way to bubble events to the upper controls and catch them. The first thing you should look at is the OnBubbleEvent protected method of the Control object. It catches all the events raised by its controls. Override it and you can catch events and treat them. Now, this method returns a bool value. It tells the RaiseBubbleEvent method (I'll talk about it shortly) if the event was handled or not. If it was, then the event doesn't rise above that control.

Now, about the RaiseBubbleEvent method, also a protected method of the Control object. What it does is go up the object hierarchy and execute on each object the OnBubbleEvent. It also stops if the method returns true. It is so simple and perfect that it is not overridden in any of the ASP.NET 2.0 WebControls.

But there is a catch. Some objects, like DataLists, DataGrids, Gridviews, Repeaters, etc, choose to handle the events in their items no matter what. That's why if you click a button inside a DataList, the event won't bubble to the OnBubbleEvent method from the UserControl or Page the DataList is in. Instead, the event reaches the OnItemCommand event.

So, if you want to use this general bubbling method with WebControls that override OnBubbleEvent, you have two options.
One is use inherited objects that remove this issue altogether (like inheriting from a DataList, overriding OnBubbleEvent, executing base.OnBubbleEvent, then always return false), which might make things cumbersome since you would have to define a control library.
The second option is to always handle the OnItemCommand and other similar events with something like RaiseBubbleEvent(sender, args); . This very line will re bubble the event upwards.

Benefits: the benefits of using this general event bubbling technique is that you don't have to always specifically write code in the OnItemCommand or having to handle OnClick events and so on and so on. Just catch all events, check if their arguments are CommandEventArgs, then handle them depending on source, command name and command argument.

Thursday, 25 January 2007

The Twin Towers, national testicles!

I know it's bad taste, but it was too good not to blog about it. I just had this vision of this school bully, pushing kids around, hitting and scaring them. One day, one of the kids fights back and kicks the bully in the nads. So he crumbles to the floor, but then tells everyone how that attack was unmanly and cowardly and then beats the crap out of the kid.

The same thing happened with the 9/11 thing. Two every important American items, symbolically destroyed to show the otherwise almighty US that they can be hit, even by the little people. It hurt like hell and it made everybody angry, but it also made the point.

The vision of the twin towers, as giant testicles (with small sperm inside?) felt hilarious to me...

Tuesday, 23 January 2007

Piata Unirii (Unity Square) in Bucuresti (Bucharest)

Agent Smith from Matrix once said "I hate this place. This zoo. This prison. This reality, whatever you want to call it, I can't stand it any longer. It's the smell, if there is such a thing. I feel saturated by it. I can taste your stink and every time I do, I fear that I've somehow been infected by it" and that made me like him instantly.

He described perfectly how I feel whenever I exit the subway or bus at Unirii and go towards the tramway 32 station. It's only 100 meters, maybe 150, but you have to fight your way through a river of people, a lot of them smoking, then wait for the green semaphore light a long time while smelling all kind of interesting flavours, then try to get in the tram without being rubbed away like a pencil trail by people who don't really care if they touch you or not.

If you are lucky enough and it's winter time, maybe before some holiday or another, you get to smell the homeless people who decide that either it's too cold outside and camp in the tram, either that you have to give them a holiday present and start begging and singing and other things which basically mean they also move or crawl, thus evenly smearing the air with their fetid stink.

Dance, Dance, Dance by Harumi Murakami

The lead characters of this author seem to always be quiet guys, ready to accept anything and be taken anywhere by the story. The same incredible things happen, all narrated in a very calm way, making them real. The obsession for American or English music permeates everything. I've read two Murakami books, but also some reviews for other books, and the same situation seems to be repeating itself.

Dance, Dance, Dance is about this kind of a person, caught in a kind of metaphysical quest to find himself. Some symbolism, weird characters, absurd situations. In the end, surprise, he finds himself. Compared with Kafka on the Shore it is much slower at the beginning, but it picks up pace closer to the end, yet the very end is slightly different from the rest of the book. I first though that Kafka was a better book than this one, then I switched sides and now I see them as of similar quality. Not much else to say.

Now I can't say I didn't like the books. They are the kind of stories that take you nowhere, but on a very pleasant road. But in the end, you are left with nothing. Nothing really learned, nothing really gained, just like a walk in the park. And I rarely enjoy walks in the park.

So I have decided to call it quits. Harumi Murakami is a good writer, there is no doubt about it, just not my type of a writer. I feel his writing is too English and not Japanese enough, thus defeating the very purpose I started to read his books in the first place.

Monday, 22 January 2007

Use different templates in the same DataList

First of all there is no reason not to apply this method to any other templatable control like GridViews or DataGrids. The basic mechanism is the same.

The problem here is how to display different templates for different items (not just for different item types) like when databinding to a general collection of different objects.

Basically all you have to do is use the OnItemCreated event to first load a template (using Page.LoadTemplate) from any user control and then applying it to the DataListItem (using ITemplate.InstantiateIn). But there are a few catches. First of all, the original templates (the ones in the as?x file) are applied before the OnItemCreated event. So if some templates are defined, like ItemTemplate for example, you need to clear the controls in the item before you instantiate your template. Second of all, you cannot declare the OnItemCreated event in the Page_Load method, since it is executed after the OnItemCreated event. But you can declare it in the as?x file.

So let's combine all this into code. First the aspx:
<asp:DataList id="dlMain" runat="server" OnItemCreated="dlMain_ItemCreated" >
</asp:DataList>

nothing fancy. Then the cs code:
protected void dlMain_ItemDataBound(object sender, DataListItemEventArgs e)
{
ITemplate template1 = e.Item.ItemIndex % 2 == 0
? Page.LoadTemplate("~/ucTest1.ascx")
: Page.LoadTemplate("~/ucTest2.ascx");
e.Item.Controls.Clear();
template1.InstantiateIn(e.Item);
}


That's it! ucTest1 and ucTest2 are two arbitrary user controls I am loading alternatively. For this particular case an AlternatingItemTemplate could have been used, but you get the point.