First of all, to turn a normal ASP.NET application to Ajax takes only a few minutes with the Microsoft Ajax.Net platform. It's as easy as moving some of the content in UpdatePanels and adding a ScriptManager. Of course, you need the Ajax.Net package installed.

What I am going to talk about is a simple way to enable/disable Ajax, and keeping the entire basic functionality intact. Why would I do that? Because sometimes you need to see the project working on a computer that doesn't have the Ajax framework installed. You might even want to work on the project itself. So this is what you do:

First of all, the ScriptManager control must appear on every Ajax page in the project, so why not move it to the MasterPage? Yes. Only one ScriptManager in the MasterPage suffices. Second of all, the UpdatePanel and the UpdateProgress controls are nothing more than normal Panels with the cool Ajax functionality added to them. So enabling or disabling Ajax surmounts to nothing more than replacing these controls with normal panels.

So here is the quick method of enabling, disabling Ajax.Net whenever you want:
Start off from the Ajax enabled application and save the web.config as web.config.ajax. Remove everything from it that resembles System.Web.Extensions and all other weird things that Ajax.Net adds to the web.config except this:
<system.web>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>

this you only replace with this:
<system.web>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="MockAspNetAjax"/>
</controls>

Save it to Web.config.noajax

You might see where I am going already. Now create a new Visual Studio project, a class library, one that you will use for all these conversions, and call it MockAspNetAjax. Go to the project Properties and change the default namespace to System.Web.UI. Add three classes that do nothing but inherit from Panel: ScriptManager, UpdatePanel, UpdateProgress. The UpdateProgress will have some additional code:
 public UpdateProgress()
{
Init += new EventHandler(UpdateProgress_Init);
}

void UpdateProgress_Init(object sender, EventArgs e)
{
Visible = false;
}

because you don't want to see the Ajax Update Progress message continuously on your page.

In order to convert an ASP.NET Ajax application to a normal postback application you follow two simple steps:
1. overwrite the web.config with web.config.noajax
2. add MockAspNetAjax as a reference or include in the project if previously excluded.

back to Ajax:

1. overwrite the web.config with web.config.ajax
2. remove MockAspNetAjax as a reference or exclude the dll from the project, while keeping the dll there.

That's it!

Of course, Ajax has a lot more stuff to it, like for example the [ScriptService] web services that are directly accessed from Javascript. Or Ajax enabled controls or other controls which don't even work without the framework. These are more complex situations which cannot be solved with such a general solution, but the same basic principles apply: use web config to avoid Register tags in each page, replace controls with mockup controls, remove HttpHandlers and HttpModules, replace javascript code that uses Ajax with something that does nothing or emulates the same behaviour (preferably) through postbacks or hidden iframes, etc.

In ASP.NET a Control has a fully qualified id that can be deduced from the control hierarchy and can be accessed with the properties ClientID or UniqueID. It then becomes the unique id or name of rendered html controls. It makes sense that these properties should be used right after the control hierarchy is completely defined, that means before the rendering, therefore in the PreRender.

What is not so well known is that accessing those two properties sets the _cachedUniqueID member, which sets irrevocably the ID to a control. That's why using these properties in ItemCreated events, for example, makes the html id of controls to remain the default defined one.

Example: In a DataList, you have an item template that contains a control, let's call it Control1. The rendered id in html will look like this: ctl00_ContentPlaceHolder1_UcUserControl_DataList1_ctl00_Control1 , but if you use ClientID inside the DataList_ItemCreated event, the rendered html id will be just Control1, thus making any javascript manipulation futile.

Of course, one could create a method to return the UniqueID without setting the cached value, since there are moments when the partial hierarchy is enough to define a proper id. Unfortunately, for controls without a specific and declared id, ASP.NET creates and automatic ID like ctl[number] or _ctl[number] and, of course, those methods and fields are all private or internal. One could use Reflection to get to them, but what would be the point?
UniqueID and ClientID are overridable, though, so one can change their behaviour in user defined controls.

Related links:
Accessing ClientID or UniqueID too early can cause issues

Long story short, here's the link:
Inserting at the cursor using JavaScript

and has 0 comments
Today Orange Romania had organised a concert in their brand new "Concept Store". A Concept Store is exactly what it says it is, a place where they sell you an idea, the idea of buying and using Orange phones and subscriptions. Therefore everything that takes place in that store is directly linked to image and marketing.
Well. today they blew it, royal style. 15 minutes before the start of the concert (Coma was going to perform, they are this extremely cool Deftones meets Linkin Park via Tool band) they announced that the concert was not going to take place due to complaints from the neighbours. What actually happened is that some greedy idiots thought they could get some money from Orange because they lived right above the Orange Concept Store. Well it didn't work, but the funny thing is they announced the cancellation of the concert after keeping tens of fans out in the cold while watching them through the glass doors of the place. And these were only the ones that got there earlier. I got there about 30 minutes before the announced hour for the concert so I was amazed to see a couple of suits staring at us from the other side of the glass and doing absolutely nothing about it. Then the cancellation news came.
The band members were devastated, the fans disappointed, Orange made complete fools of themselves. But will this ever reach the news? NO! There is no rape or murder involved, no political scandal. Just a few ordinary teens were completely stepped upon in the name of Orange Corporation Choppers!

I had this DataList with a simple ItemTemplate which contained a Select button. I wanted to click on the item and select it, without the use of the button. So what I s did is add this in DataList1.OnInit:
Panel1.Attributes["onclick"]=Page.ClientScript.GetPostBackEventReference(btnSelect, "");

Nothing was working after that. The select button didn't fire the OnItemCommand event and its ClientID was just btnSelect, not ctl00$ContentPlaceHolder1$UcGroupEdit1$DataList1$ctl00$btnSelect like it should have been. Of course, even considering the possibility of a button firing a command event when there are ten other buttons with the same ID on the page was ridiculous. What has caused this?

Well, I removed the line above and everything worked again. The only solution was to override the user control Render method, then take each item, FindControl the button and the panel, then repeat the same line, with an additional parameter that said to register the control for postback. If I didn't do that, an event validation error would have occurred. Why Render? Because using the last parameter anywhere else causes the "RegisterForEventValidation can only be called during Render();" error.

So
Panel1.Attributes["onclick"]=Page.ClientScript.GetPostBackEventReference(btnSelect, "",true);
in the Render event was the only solution. I've spent hours just understanding what is wrong. Grrr!

Update: I think the post Problems when using ClientID or UniqueID before the PreRender event is closer to the problem and its causes.

and has 1 comment
Caught in a desire to be more scientist-like I've borrowed the book "I Am a Mathematician" by Norbert Wiener from a friend of mine. While being a rather old book (written in 1956), it was exactly what I was looking for: a book that described in layman's terms what the life of a true scientist is like and how he thinks.

I think the book itself was rather boring, but the world described and the way this guy was thinking really opened my eyes to things I wish I understood in my early teens. He sees, for example, the way sciences come together in one big thing called science. Even if he was a mathematician, he worked in physics, psychology and electronics, because he saw the way they worked together, not as separate unconnected subjects. He was thorough, focused and science minded. He went to the beach and thought about equations to define the movements of waves as they break against the shore.

My conclusion is that it is a wonderful insight in the mind of a scientist. It is not a popular science book, it is an autobiography, so it might get a little boring, but it also puts everything into context.

and has 0 comments
No Comment

and has 1 comment
"Hey!", you will shout, "You are Romanian, what do you know about the American mind?". And you would be right. I don't know much about the American mind, but I do know what I see on the news and in the movies. And three movies I've recently seen opened my eyes even more.

First of all This Film Is Not Yet Rated talks about the MPAA and the way they enforce the ratings on movies. You might think that is not so important, but each rating brings with it a different possible audience size. There is a huge difference between the last two ratings R and NC-17 because an NC-17 movie doesn't get picked by distribution companies, nor does it reach into theaters.

What does that mean? It means people in the entire USA see nothing but what the MPAA deems "decent". Statistics show that in the last 50 years they mostly rate R violent movies, no matter how violent, while NC-17 are films which involve sex or even the manifestation of sexual pleasure. Do you think that has any connection with the fact the average American is a violent and militaristic puritan? Interesting enough, the MPAA "council" is completely secret. Movie directors get answers like "your film is NC-17 and we don't tell you who we are and why we rated it like this". Also, check out how the MPAA is financed by the biggest 7 film studios in Hollywood, which own 95% of all movie industry in the US and belong to conglomerates that own 90% of the entire US media!

Another interesting fact is that in that council there are two religious representatives, which brings me to the next two movies: Jesus Camp and The God Who Wasn't There.

Jesus Camp is a pretty famous documentary, because it was mostly comprised of filmed video footage and there were almost no comments in it, yet it spun a lot of controversy. Fundamentalists praised it and so did Anti-fundamentalists and atheists alike. It showed how children are entranced and then conditioned to believe in Creationism, to actively oppose abortion (when they are children), to support Bush and to think that they must act as soldiers that bring America back "under God". This camp teaches the Pentecostal religion, maybe only an extreme version of it, but what is majorly important in this religion is the Rapture. The Rapture is the second coming of Jesus, which will quickly take all true believers to Heaven, then end the world. Maybe even reformat the hard drive. 22% of Americans are Pentecostals who believe Jesus will come during their lifetime. Why wonder Americans don't really care what happens to the Earth? A major Pentecostal preacher is a weekly counselor for the White House and, interviewed in this movie, said that if Evangelicals vote, they determine the outcome of US elections. Pretty scary stuff. Even more scary is the fact that they are a big religion with between 40 and 80 million adepts and no one can do anything about this child brainwashing that they do in this camp (and a lot of others, I guess). David Byrne, from Talking Heads, said in his blog that he saw no difference between this camp and the extreme Muslim madrases training people to blow themselves up.

The God Who Wasn't There starts off like an atheist movie that proves most of Christianity is copied from other older religions, including details, and that average common sense makes believing in a religion like this kind of stupid. The special effects and direct attacks against religion made me think that this is an extreme in itself. Atheists turned anti-theists and throwing everything they have at poor people that believe stuff like that. This until the guy admitted he was a Christian Fundamentalist until the age of 15, when he actually decided everything he was taught under threat of eternal damnation was plain stupid.

OK. Back to the main point. Now I understand more about the American Mind. I understand how a nation that created the ideas of secular state, individual freedom and freedom of faith can also be hard ass conservatives, God fearing and warmongers in the same time. How most American morality is ridiculously naive when concerning sex while in the same time the US is producing the most adult content in the world. I realize why some of the Americans I talk to are really smart and funny people and others sound like rednecks from old Westerns, why US science is the most advanced in the world while in the same time regulated by people following 1st century morality.

Therefore I highly recommend seeing these films. Eye openers.

and has 0 comments
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.

and has 0 comments
I have found this very cool game while browsing the net. While I personally hated Sonic games, I found this one strangely interesting. The minimalist design probably did it for me. Anyway, check it for yourselves.

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.

and has 1 comment
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...

and has 0 comments
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.

and has 0 comments
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.

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.