Thursday 26 June 2008

Text is not wrapping in my fixed width div!!

Now, this is a stupid post. And By stupid I mean that kind of thing that if you hear about it you think "Oh, it was obvious", but if it happends to you you waste a lot of time trying to fix, then you think "Oh, it was obvious. If I blog about it I will sound stupid". Well, this kind of blog posts are the most important, I think, so here it is, sounding stupid:

I had a div, absolutely positioned and of fixed width, with a long text inside that would just not wrap! That means the div would either expand its width to accomodate my text (Internet Explorer) or bleed the text out (FireFox). Was it white-space:nowrap? No! Was it that some of the table cells that contained elements had the "noWrap" attribute? No! What the hell is going on?!

Solution and explanation: wrapping occurs only on NORMAL SPACES, not  . The text inside my div didn't really have any spaces in it, it was one continuous string of words joined by the innocent  .

Gantz - Weird but fun manga

I was looking for new manga to read on the OneManga site and I found Gantz. It has senseless violence, gratuituos sex scenes and great looking chicks in erotic positions. The concept is that a weird black ball is taking people right from before they die and brings them to a room from which they are sent to battle aliens in the streets of Tokyo. Normal people can't see them or the monsters, but they can be killed by them. And they often are.



This is something only a Japanese could have thought of. People are dying, most people around don't care and they are all trying to show how superior they are compared to others. And then they find something to PROTECT and they cry all the time.

Bottom line: Monsters, Aliens, Vampires, Hot chicks, Sword fighting, Gore, Sex, Rape, Emotional torture... they are all in there. The script doesn't make much sense, though, and I think all characters are emotionally stumped to the level of three year olds. That's how Naruto and InuYasha won so many fans, through carefully crafted emotional landscapes, something Gantz lacks almost completely.

Read Gantz at OneManga.
imDb link for the anime

Saturday 21 June 2008

User Experience Workshop - Sinaia 15th-20th June 2008

It was great! Not only the setting was nice (the four star Smart hotel is exactly what I had expected a hotel should be, except the restaurant, maybe), but the weather was cool, the presentation helpful, the tutor (Aurelian Popa) was above expectations and the people pleasant. Not to mention a week away from boring stuff. ;) I feel it would be pointless to detail what we did there, since it was either my own personal life or the actual workshop (which involves work), so I will give you some impressions of the technology and point you towards the resources that would allow you to go through the same learning process.

The whole thing was about WPF and SilverLight and I can tell you two conclusions right now:
WPF/XAML/SilverLight are a great technology and I expect a lot of .Net applications to migrate towards it in the next 6 to 12 months.
The complexity of this technology is likely to put a lot of people off, therefore the tools like Expression Blend and the Visul Studio interface become completely indispensable and must evolve to have great ease of use and become more intuitive.

The entire presentation model allows one to use any graphical transformation available, including 3D, on any part of the interface. The controls are now without appearance. They come with a default appearance that can be totally replaced with your own. A weird example is to use a 3D cube with video running on each side as a button. Of course, the whole thing is still work in progress and some stuff is yet difficult to do. Besides, you know Microsoft: a lot of complicated things are easy to do, while some of the simplest are next to impossible.

You can taste the Microsoft confidence on this by watching them release an entire design oriented suite (Expression) and working on making Silverlight available on all platforms and browsers. Just the fact that Silverlight can access directly the browser DOM is enough to make me remove all those patchy javascript scripts and replace them with nice Silverlight C# code.

Enough of this. Go learn for yourself!

Tools:
Silverlight is at version 2 beta 2. That is painfully obvious when new bugs are introduced and beta 1 applications break. The Expression Blend tool is at version 2.5 June 2008 CTP and it has also a long walk ahead towards becoming useful. Visual Studio 2008 performs rather well when faced with XAML and WPF stuff, but the Resharper 4.0 addon helps it out a lot. You need the Visual Studio 2008 Silverlight Tools, too. After this compulsory tool kit you could also look at Snoop, Blender and Expression Deep Zoom Composer.

Learning material:
Simplest thing to do is to go to Silverlight Hands-on Labs or download the WPF Hand-on labs and download them all and run through the documentation script that is included with each one. There are video tutorials about how to use the tools, too. Here is one for Blend. Of course, all blogs and materials available online at the search of a Google are helpful, as well.

Community:
As any community, it depends on your desired locality and interests. You can look for local .Net / WPF groups or browse for blogs half way around the globe from you. From my limited googling during the workshop I can see that there are people talking about their issues with WPF and SL, but not nearly enough: the technology still needs to mature. I haven't really searched for it, but I've stumbled upon this site: WindowsClient.NET that seems to centralize WPF, Windows Forms and a bit of Silverlight information.

Inuyasha ends

A while ago I was recommending the anime called Inuyasha and then reading on the manga. Well, after 558 episodes - each having around 19 manga (comics) slides - Inuyasha has reached the end. A bit anticlimactic, considering the things that attracted me to the story in the first place, but an end nevertheless.

You can read the entire story at Onemanga.

Sunday 15 June 2008

Templated Web User Controls

I wanted to write this great post about how to make Web User Controls that would have templates, just like Repeaters or GridViews or whatever, face any problems, then solve them. Unfortunately, MSDN already has a post like this: How to: Create Templated ASP.NET User Controls. So all I can do is tell you where to use this and what problems you might encounter.

I think the usage is pretty clear and useful: whenever you have as?x code that repeats itself, but has different content, you can use a templated Web User Control. The best example I can think of is a collapsable panel. You have a Panel, with some javascript attached to it, a hidden field to hold the collapse state, some buttons and images and texts to act as a header, but every time the content is different.

Now with the issues one might encounter. In Visual Studio 2005 you get an error, while in VS 2008 you get a warning telling you the inner template, whatever name you gave it, is not supported. This is addressed by the
[ PersistenceMode(PersistenceMode.InnerProperty) ]
decoration of the ITemplate property of the control.
Then there is the issue of the design mode, where you get an ugly error in all Visual Studio versions: Type 'System.Web.UI.UserControl' does not have a public property called '[yourTemplatePropertyName]'. As far as I know there is no way of getting rid of this. It is an issue within Visual Studio. However, the thing compiles and the source as?x code is warning free. I think one could easily sacrifice some design time comfort to reusability.

The politics of Bucharest show arrogance does not win

I am not usually one to talk politics, especially since I don't really think there are essential differences between people participating in this game. Yes, I do see it as a game, with rules that you need to follow to get the prize. But this year's mayor election proved that some of the rules are more subtle than just spending the biggest amount of money in promotional ads and having the biggest party support you. Well, pretending to be "one of the people" seems to always help, though. :)

What happened? Internal struggles within the main opposition party led to them choosing their candidate for the city hall not the man with the most popular votes (as resulting from opinion poles), but the man with the most connections in the party. Therefore the other guy decided to candidate independently. He spent almost nothing on campaign ads, while the leading party candidate spent about 600000 euros just for the first part of the elections and God knows how much for the final part.

Conclusion? Sorin Oprescu, the independent candidate, has won the elections. The leading party candidate lost, with all his ad money, while no one even noticed the candidate from the oposition party. Apparently, a great victory for the people in Bucharest. I have no idea if the guy will be any good as a mayor, and I think that is the major flaw in Romanian elections, but the arrogant belief that party support and lots of money can just land you in a popular position has failed once more in Bucharest today.

But my theory is that Oprescu didn't win just by charisma or by the total lack of charisma of his opponent, Blaga, but from the ugly and cheap attacks against him and other candidates from the main party. With slogans like "Let's get rid of the garbage in sector 5, dump Vanghelie" and images of a bulldog and a snake with glasses (Blaga looks like a big ugly dog, while Oprescu wears glasses) they pushed people away. I guess that the fact that the snake is a symbol of wisdom in many cultures past them by completely.

Anyway, my conclusion is that arrogance is the worst thing a Romanian politician can do right now. They can be stupid, corrupt, pathetic, but NOT arrogant. It is traditional in Romania to dream to become powerful, rich, above all others, and it is even more traditional, since most people never do get rich or famous, to totally despise and hate the people that do or behave like they do. Today was a lesson in humility for the political class.

Saturday 14 June 2008

Very slow UpdatePanel refresh when containing big ListBoxes or DropDownLists

Update: this fix is now on Github: Github. Get the latest version from there.

The scenario is pretty straightforward: a ListBox or DropDownList or any control that renders as a Select html element with a few thousand entries or more causes an asynchronous UpdatePanel update to become incredibly slow on Internet Explorer and reasonably slow on FireFox, keeping the CPU to 100% during this time. Why is that?

Delving into the UpdatePanel inner workings one can see that the actual update is done through an _updatePanel Javascript function. It contains three major parts: it runs all dispose scripts for the update panel, then it executes _destroyTree(element) and then sets element.innerHTML to whatever content it contains. Amazingly enough, the slow part comes from the _destroyTree function. It recursively takes all html elements in an UpdatePanel div and tries to dispose them, their associated controls and their associated behaviours. I don't know why it takes so long with select elements, all I can tell you is that childNodes contains all the options of a select and thus the script tries to dispose every one of them, but it is mostly an IE DOM issue.

What is the solution? Enter the ScriptManager.RegisterDispose method. It registers dispose Javascript scripts for any control during UpdatePanel refresh or delete. Remember the first part of _updatePanel? So if you add a script that clears all the useless options of the select on dispose, you get instantaneous update!

First attempt: I used select.options.length=0;. I realized that on Internet Explorer it took just as much to clear the options as it took to dispose them in the _destroyTree function. The only way I could make it work instantly is with select.parentNode.removeChild(select). Of course, that means that the actual selection would be lost, so something more complicated was needed if I wanted to preserve the selection in the ListBox.

Second attempt: I would dynamically create another select, with the same id and name as the target select element, but then I would populate it only with the selected options from the target, then use replaceChild to make the switch. This worked fine, but I wanted something a little better, because I would have the same issue trying to dynamically create a select with a few thousand items.

Third attempt: I would dynamically create a hidden input with the same id and name as the target select, then I would set its value to the comma separated list of the values of the selected options in the target select element. That should have solved all problems, but somehow it didn't. When selecting 10000 items and updating the UpdatePanel, it took about 5 seconds to replace the select with the hidden field, but then it took minutes again to recreate the updatePanel!

Here is the piece of code that fixes most of the issues so far:
/// <summary>
/// Use it in Page_Load.
/// lbTest is a ListBox with 10000 items
/// updMain is the UpdatePanel in which it resides
/// </summary>
private void RegisterScript()
{
string script =
string.Format(@"
var select=document.getElementById('{0}');
if (select) {{
// first attempt
//select.parentNode.removeChild(select);


// second attempt
// var stub=document.createElement('select');
// stub.id=select.id;
// for (var i=0; i<select.options.length; i++)
// if (select.options[i].selected) {{
// var op=new Option(select.options[i].text,select.options[i].value);
// op.selected=true;
// stub.options[stub.options.length]=op;
// }}
// select.parentNode.replaceChild(stub,select);


// third attempt
var stub=document.createElement('input');
stub.type='hidden';
stub.id=select.id;
stub.name=select.name;
stub._behaviors=select._behaviors;
var val=new Array();
for (var i=0; i<select.options.length; i++)
if (select.options[i].selected) {{
val[val.length]=select.options[i].value;
}}
stub.value=val.join(',');
select.parentNode.replaceChild(stub,select);

}};"
,
lbTest.ClientID);
ScriptManager sm = ScriptManager.GetCurrent(this);
if (sm != null) sm.RegisterDispose(lbTest, script);
}


What made the whole thing be still slow was the initialization of the page after the UpdatePanel updated. It goes all the way to the WebForms.js file embedded in the System.Web.dll (NOT System.Web.Extensions.dll), so part of the .NET framework. What it does it take all the elements of the html form (for selects it takes all selected options) and adds them to the list of postbacked controls within the WebForm_InitCallback javascript function.

The code looks like this:
if (tagName == "select") {
var selectCount = element.options.length;
for (var j = 0; j < selectCount; j++) {
var selectChild = element.options[j];
if (selectChild.selected == true) {
WebForm_InitCallbackAddField(element.name, element.value);
}
}
}
function WebForm_InitCallbackAddField(name, value) {
var nameValue = new Object();
nameValue.name = name;
nameValue.value = value;
__theFormPostCollection[__theFormPostCollection.length] = nameValue;
__theFormPostData += name + "=" + WebForm_EncodeCallback(value) + "&";
}
That is funny enough, because __theFormPostCollection is only used to simulate a postback by adding a hidden input for each of the collection's items to a xmlRequestFrame (just like my code above) in the function WebForm_DoCallback which in turn is called only in the GetCallbackEventReference(string target, string argument, string clientCallback, string context, string clientErrorCallback, bool useAsync) method of the ClientScriptManager which in turn is only used in rarely used scenarios with the own mechanism of javascript callbacks of GridViews, DetailViews and TreeViews. And that is it!! The incredible delay in this javascript code comes from a useless piece of code! The whole WebForm_InitCallback function is useless most of the time! So I added this little bit of code to the RegisterScript method and it all went marvelously fast: 10 seconds for 10000 selected items.
string script = @"WebForm_InitCallback=function() {};";
ScriptManager.RegisterStartupScript(this, GetType(), "removeWebForm_InitCallback", script, true);


And that is it! Problem solved.

Thursday 12 June 2008

Maintain Some Value Across Postbacks

This is mostly a noob post, but I had to write it because I've had to work with a project written by a colleague of mine and her method of maintaining value across postbacks was to use HiddenFields. I will explore that option and the ViewState option.

First of all, what are the advantages of using a hidden field? I can see only two:
1. it would work even if ViewState is disabled
2. its value is accesible through javascript
The disadvantages are:
1. it creates additional HTML markup
2. it can only store stuff in string format
3. its value is accesible through javascript

I would not use the hidden field option mainly because it gives people the ability to see and change the value through simple javascript manipulation. It's a security risk, even if most of the times you don't really care about the security of some value maintained through postback. I would use it only when _I_ need to change that value through javascript.

For some code, I assume I want to store an integer value called MyValue. There will be a field called _myValue that will store the value during a cycle, but it is used mainly for caching (reading Request and ViewState is slow) and it is declared like this:

private int? _myValue;


Now, about the structure of such a code. The simplest method is to actually create a (or many) hidden field(s) in your page. You can then use the values directly. It is simple, but hardly maintainable:

Markup:

<asp:HiddenField id=hfMyValue runat=server>

C# code:

public int MyValue
{
get
{
if (_myValue == null)
{
if (String.IsNullOrEmpty(hfMyValue.Value)) MyValue = 10;
else _myValue = Int32.Parse(hfMyValue.Value);
}
return _myValue.Value;
}
set
{
hfMyValue.Value = value.ToString();
_myValue = value;
}
}


I've wrapped the functionality of the hidden field in a property so I can easily use it through my code.

Another method of doing this is to use the RegisterHiddenField method of the ScriptManager like this:

C# code only:

public int MyValue
{
get
{
if (_myValue==null)
{
if (Request["MyValue"] == null) MyValue = 10;
else _myValue = Int32.Parse(Request["MyValue"]);
}
return _myValue.Value;
}
set
{
PreRender -= MyValue_Registration;
PreRender += MyValue_Registration;
_myValue = value;
}
}

void MyValue_Registration(object sender, EventArgs e)
{
if (_myValue.HasValue)
ScriptManager.RegisterHiddenField(this, "MyValue", _myValue.Value.ToString());
}


As you can see, there is no need of my changing the markup. There is the ugly part of having to attach to the prerender event to register the hidden field because the ScriptManager doesn't have any way of accessing the registered hidden field after you did it or at least a way to un-register it. Registering it again doesn't change its value, either.

In both these cases the value is accessible through javascript:

<script>var myValue=parseInt(document.getElementById('<%=hfMyValue.ClientID%>').value);</script>
<script>var myValue=parseInt(document.getElementById('MyValue').value);</script>


But there is an easier way of storing the values through postback, and that is by using ViewState. In order to do that, your object needs only to be Serializable. It can be anything from a string to a complex DataSet. There is no way to access it through javascript, though. Here is the C# code for it:

public int MyValue
{
get
{
if (_myValue == null)
{
if (ViewState["MyValue"] == null) MyValue = 10;
else _myValue = (int)ViewState["MyValue"];
}
return _myValue.Value;
}
set
{
ViewState["MyValue"] = value;
_myValue = value;
}
}


Doesn't that look a lot simpler? And the beauty of it is, the ViewState can be sent inside the markup, as in the default behaviour, but it can just as easily be stored on the server, either by using a SessionStatePersister or by other ways.

Update: Also, a more complicated, but a lot more flexibile way of doing things is described on the DimeBrain blog: Frictionless data persistence in ASP.NET WebForms.

Exultant by Stephen Baxter

book coverExultant, the second book in the Destiny's Children series felt a lot better than Coalescent. Not without its own flaws, it made the entire experience better, but maybe that's just me.

The book describes a universe twenty thousand years into the future, when human kind has infested the galaxy, destroying all sentient races they encountered with their immense war machine. They are currently at war with a technologically superior enemy called the Xeelee, which are trapped at the core of the galaxy, pushed back by the sheer size of human forces. The war has waged for 3000 years and continues with no advancement of any kind, with the entire human philosophy focused on spewing more and more cannon fodder for a war that is neither to be won or lost, just endured.

A rather bleak vision of the future, but fear not, there comes hope! Somehow, an excentric aristocrat comes with all the ideas and resources to create the ultimate weapon that will destroy the Xeelee! And in the pages of the book it is described how they go at it. This is where the book actually fails, because at a such immense space and time scale, a solution of this simplicity is just not believable. You don't feel it in your GUT! But the book is well written, the style bringing memories of Asimov, and the ideas in it pretty interesting.

Stephen Baxter is again applying Universal Darwinism to his universe, bringing more and more species and types of lifeforms out of his magician hat. The ending of the book is terribly naive, but without a bit of naivite, you cannot finish great space sagas in a single book.

Bottom line: if you like space fights, military stratagems, character development, time travel, large scale galactic intrigues and a lot of techno babble (and I know I do! :) ) you will love this book. I do think that some of the great ideas in the book would have mixed nicely with late David Feintuch's writing. Anyway, on with the next book in the series: Transcendent

Tuesday 10 June 2008

The Bucharest Bicycle Lane Scam

A year ago I wrote about being a bicycle rider in Bucharest. I complained then that there are no bike lanes and no one gives a damn about bicycle riders or the very law that should protect them. Things have changed, apparently, and now beautiful bike lanes are criss-crossing Bucharest's sidewalks. But it is all for show and appearance.

First of all, they serve no purpose on the sidewalk. Given the peculiar psychology of the majority of the population when they have to choose between walking on a nicely marked yellow/green stripe and the normal gray sidewalk, they will walk where the nice colors are. Even if they would somehow decide to think a little and ponder on the significance of the big bike signs painted on those lanes, there are children that have no idea what they are doing and could always jump in front of you. Normally, the population of the bike lanes in bucharest is comprised of mothers with babies, lovers holding hands, old ladies and people that just wouldn't give a damn. Very often I see young stylishly dressed women walking right towards my bike, ignoring me completely, expecting me to move aside. Do they even consider the damage that I would do if I'd just decided on a whim NOT to move aside? Not to mention places where there is space only for the bike lane and it would be stupid to expect people not to walk on it. And of course, the cars that park right on the lanes and about nobody does anything.

Second of all, the lanes were built close to autumn last year, a season not favourable to biking. Then, of course, winter came, and the plastic like stripes that mark the lanes were pretty much destroyed. So they had to put them again. However, during the spring they started working on fixing the streets. That meant scraping the asphalt (bike lanes on the street included) and puting it back on. But they did draw the lanes back on. Then they started changing the street border stones, usually placing them idiotically high right on the bike lane. And if it wasn't enough, they started placing all above ground wires under ground. That meant digging narrow, yet deep trenches in the sidewalk, interrupting the lanes from side to side, so that there is no way to go around, and they never filled them up! Basically every one in two bike lanes is now broken by this. And, as this was not enough, more often than not the lane is of worse quality than the rest of the sidewalk.

Therefore I will ignore these lanes completely, except the days when I am really tired and don't trust myself around cars, and risk my life and the paint on the cars around me rather than the lives of the people on the sidewalk. What a sham this all is. The Bucharest European mask is there only for outsiders, NOT for the people living in it.

Sunday 8 June 2008

Coalescent by Stephen Baxter

In 1973, Frank Herbert wrote a book called Hellstrom's Hive in which it described a sect of people that lived underground, in a system much alike insects, with individuals specialised for different tasks and all living for the big hive organism. The book did not explain how it all got there, it just quickly described the situation and then delved into the action.

Book cover Jump to Stephen Baxter's Coalescent, the first book of the Destiny's Children series, which pretty much details how a group of humans would reach a plausible hive like society. Unfortunately, the book is more descriptive than anything else, failing to deliver in the action part. A lot of characters are developed and a lot of history (both personal and general) is detailed, but in the end the characters vanish as if they never mattered. It is, after all, the whole point of the novel, that ignorant individuals following certain rules lead to the emergence of patterns, but it did not fit well within a book.

Not that the book itself is not fascinating and well written, because it is, but the pace is very slow at the beginning, accelerating to a snail pace in the end, while the different parts of the book seem fractured, too little related to one another. I intend to read the rest of the books in the series, but I might just give up, too.

Bottom line, I think it would be a nice read to start with Coalescent and then read Hellstrom's Hive, although I do think the second book to be much better.

Friday 6 June 2008

Google Toolbar for Internet Explorer with XP SP3 sucks!

No, it's not one of the The World Sucks rants, it's actually very serious. I was terribly annoyed that after installing Windows XP SP3 my Internet Explorer 7 browser would (sometimes) freeze when closing tabs. I could replicate the bug easily enough by folowwing these steps:
  1. Open Internet Explorer
  2. Go to http://siderite.blogspot.com
  3. Scrollwheel click on Sign in, thus opening it in another tab
  4. Wait until the title of the second tab changes to Redirecting (because I have the cookie from a previous login)
  5. Press F5 (Refresh) in the first tab
  6. Click on the second tab and close it while the first tab is refreshing


At this time Internet Explorer would freeze with no error message. Waiting for a few seconds I could access the context menu on the taskbar button and choose Close or click on the X Close button, but everything inside the Internet Explorer window would be inactive. If I would switch tasks back and forth, the IE window would appear completely blank inside.

Now, whenever you look for the solution for this you get three answers:
  • Check the Warn me when closing multiple tabs option in the Internet Explorer tabs settings.
  • Run IExplore.exe with the -extoff command line parameter which will disable all add-ons and this will tell you if the addons are causing this
  • Check for spyware/malware


I did all this and nothing changed! I've reinstalled all addons. I've reinstalled IE7. I've switched to IE8 beta! Nothing worked. Then I proceeded in uninstalling all addons, see what happends. Finally, it wouldn't cause any issues. It was just after I've uninstalled Google Toolbar! I suspect the cause for the bug is coming from the popup blocker which interprets tabs as popups and tries to close them. Then there is the setting in GT that is trying to preserve any software from modifying its settings. Maybe that is why -extoff doesn't affect it!

Just to be sure I installed it again and tested the bug and I could replicate it. Something in Windows XP Service Pack 3 messes Google Toolbar up!

Bottom line: uninstall Google Toolbar until I can find out which part of it actually causes the bug.

Monday 2 June 2008

YouTube is going down

As I was writing in the previous post, because of that law suit they are in, Google is starting to take measures to protect the copyright of videos on YouTube. The result is that all the blogs and sites that embedded video content that is supposed to be copyrighted now have beautiful flash players with a play button in them, but then the nasty surprise of seeing "video not available" when pressing play. At least display the damn message from the beginning instead of forcing me to play every video on my blog to see if there are still available!

But I did that anyway, for music videos for now, and switched to (currently) unaffected French video content site DailyMotion. Of course, lots of YouTube clones are on the net and even YouTube leecher sites. I mean sites showing a wonderful error when trying to play videos that now are removed from YouTube.

So, please folks, if you find some post with missing videos or pictures or anything wrong, really, please comment on it and I will fix it. Thank you and damn all lawyers to hell!

Korn - Falling Away from Me

I really like Korn, they are heavy, melodic and the lead singer is pretty unique. I haven't posted anything by them yet because I was a teenager when I was listening to them. I remembered loving the video for Falling Away from Me and wanted to share it with my bloggies :).