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 :).