Monday, 5 February 2007

Problems when using ClientID or UniqueID before the PreRender event

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

Saturday, 3 February 2007

Orange Romania made idiots of themselves

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!

Friday, 2 February 2007

GetPostBackEventReference disables Command or Click events

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.

Norbert Wiener - I Am a Mathematician

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.

Thursday, 1 February 2007

The American Mind

"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.