Get rid of that pesky ASP.Net UpdatePanel

Updatepanel is for babies. It’s for tiny-frolickin’-little-tots who can’t tell gaga from googoo but have learnt enough to make a pouty face at mamma when she shakes a finger at them for wetting the bed. That said, I’ve hated and used ASP.Net Updatepanels in my own project. They’re the easiest way to put Ajax in a page, and if there’s a quick requirement and you don’t know crap about javascript, you can use the ASP.Net Updatepanel. If you do that, you will never be able to look a real programmer into the eyes, not at least until you get rid of that pesky updatepanel.

The updatepanel I put up on blogvani.com helped me create a very important feature: the facility to show the mostread and liked posts in a tab without a postback. Whenever the user clicked a tab, the updatepanel would fetch the latest details from the database, and even though that would take something like 4 seconds (F-O-U-R! What was I thinking!), but hey! I got a rich client application.

Okay folks, let’s cut the chase short and get to the point.

How do we get rid of that pesky ASP.Net Updatepanel?

There are handful of ways. You can grit your teeth and go for the all javascript, xmlhttprequest, true Ajax method, use one of the handy javascript libraries like JQuery, or our own ASP.Net Ajax. To implement my updatepanel get rid-get rid solution I used ASP.Net Ajax, and since that’s the only way I know how, that’s what I am going to help you with. :)

Use PageMethods

This is the way I’d have preferred, ironically I could never get my Pagemethod to work, though for all that I knew everything was set up correctly (shows ya how much I know). In an ASP.Net Pagemethod, you declare a regular public method in the code file, like so :

[WebMethod]
public string Dosomething()
{
   return “done”;
}

Notice the [WebMethod] above the function name? That’s called the WebMethod attribute. It tells ASP.Net that you want to declare a page level web service method, and that it should let you access the method from Javascript too. In this function you can do anything, call your database access code, read a file, whatever!

Then in Javascript you can access this method like so: -

function GetSomethingDone()
{
   PageMethods.Dosomething(resultCallback);
}

function resultCallback(result)
{
  alert(request);
}

ASP.Net Ajax gives you a handy little object called PageMethods that you can use to call all the functions that you’ve marked [WebMethod] inside your page. At least that’s what the theory is. Does it work? It’s supposed to, but it didn’t work for me no matter how many times I re-examined my syntax.

So what did I do then?

Use A proper Web Service

The beauty of page method is perfect encapsulation. That is, if you need to expose a function that uses stuff that’s local to a page, you don’t need to put it in a different file. All your goodies in a single shelf. No hunting! That’s beautiful for a programmer. My programming luck is legendary but it failed that one time. Try yours and if it works for you; Great!

If it doesn’t work, you’ve can always create a proper web service.

To do this, create a web service using the File–>New file menu. This will show you the ASP.Net new file dialog. Select ‘Web Service’. Two new files will be added to your project. A .asmx file to your selected folder, and a .cs file with the same name in your App_Code folder. I don’t know what you can put in the .asmx file except the declarations, but the .cs file is the place where all your web service functions go. Declare your functions like so: -

[WebMethod]
public string Dosomething()
{
   return “done”;
}

You will also have to put the ScriptService attribute on your class to make these functions work with javascript. This is how: -

[System.Web.Script.Services.ScriptService]
public class MyClass
{
   //My Code goes here
}

Inside your Javascript,  you can call your Webservice method like this.

function GetSomethingDone()
{
   MyClass.Dosomething(resultCallback);
}

function resultCallback(result)
{
  $get(“myDiv”).InnerHTML = result;
}

In this particular version of resultCallback, I am setting the innerHTML property of a DIV element that I have pre-declared inside my HTML markup. The contents of the DIV will change on the runtime without a postback to whatever is in the result. You can return your new HTML markup in the result variable.

You see how easy it is to get rid of that pesky updatepanel and put in your own, real AJAX?

Tip: To implement this strategy, you’ve got to rely on HTML DIV elements, not ASP.Net controls. Ultimately all ASP.Net controls are converted to HTML equivalents anyway. Even Updatepanels are rendered as DIVs on the browser.

Did you enjoy this article?

What, if not updatepanel?

Man! Asp.Net Ajax’s Updatepanel is so damn crappy. It’s not suitable for any sort of business process. This total postback thing is a killer. Why’d they even create it if it couldn’t match professional requirements? Is it designed for just the kiddie stuff?

Not happy! Not. Not. Not.

I am going to have to study an entirely unconvenient paradigm for this. I will write about how I either 1. sped up the updatepanel; or 2. found an alternative way to code without the total postback.

Will keep you updated!

Can’t see everything with Internet tinted glasses

On the Internet the identity is created by the self, not the society. You can be anything you want; polite or obnoxious, educated, or brutish, rich or poor, and nobody can tell. Social identities like caste, religion, skin color are not relevant on the Internet. You can be whatever, and most of the time nobody even cares what you are. In my office & home, and in malls on weekends, I live in a shielded world. Social problems like the Dalits face in India are as foreign to me as American apartheid. I just don’t feel affected.

But a lot is being said about these problems on the Hindi blogs. Whenever I go to Blogvani I find articles on the topic. People who write blogs in Hindi are from the Hindi belt where these problems have the most impact. But more than that they seem to be better connected with the undercurrents. Many of them are journalists, and they probably run into more Dalit related incidents than an average person does.

Whatever the reasons; reading the stories has made me more sensitive about the problems of Dalits, and at the same time more sensitive about the problems of the non-dalit poor.

Sometimes I say stuff like the Internet will help create a united world… differences are going to melt… and bla… bla… But equality is not going to come so easily, or so fast. It will be a long while before everyone has access to the basic education, training, and rearing that’s required to create a mind free from prejudices. Our society in India is a incubator for prejudices. Every community, sub-community harbors prejudices against anything that’s different.

This is a form of xenophobia. People feel threatened by people from different religions, castes, regions… If we can’t learn to accept these minor differences how are we going to communicate across species if that day ever comes?

The silver-lining is that the prejudices are slowly dying as the ideals of the older generation become weaker. I believe that caste prejudices are enforced quite a bit due to the rituals particular to that caste which distinguishes it from others. As those rituals and practices die, the society will be more homogenous, and people will not have so much difference to be afraid of.

But isn’t that the short path? Creating a homogenous society is like treating the symptom (differences), not the disease (inability to cope with differences). What we should really be striving for is to create a culture in which people are not threatened by differences.

Maybe that will happen eventually anyway, because we’re bombarded with exposure to differences daily in this era.

FOB: Gravity & Speed of Darkness

Completed two books today. Gravity by Tess Gerritsen, and StarCraft: Speed of Darkness. I had been reading the former since nearly 10 days; a bunch of pages at a time in the loo. Speed of Darkness I started and finished in one sitting.

gravitybook

Gravity

On the cover Stephen King says “She’s better than Palmer… better than Cook. Yes, even better than Crichton.” He’s lying, but you won’t find that out until you’ve already bought the book, and then it’s too late as far as the book publishers are concerned. I don’t know why would people give this book a good review. It’s dated, stereotypical, filled with Dan-Brownesque incidents. It’s not a book that has much to say as far as science fiction is concerned. Maybe it can be called a medical novel,  but its completely ridiculous on that front too.

The book’s protagonists are so typical that I was groaning by the time I reached the second chapter. A couple reluctantly drawn into a divorce they both can’t avoid but want to. Now where I haven’t I seen this before? Nowhere!

The astronaut goes into space, gets sick, and is dying. The doctor husband who happens to work in NASA too hauls himself up to the shuttle in a rocket, and injects her with a miraculous virus that was discovered just in the nick of time to save her while everyone else abroad the ship had already died. Chee!

To me this novel was a disaster, not to be read, but lived through. It’s bordering on being a book that I won’t even bother reviewing. In fact I wouldn’t have bothered reviewing it had it been any other day. I am a on a writing spree since a couple of days; just needing an excuse to get some text typed.

Verdict on the book? I spent 50 cents buying it. Maybe you can buy a lollipop instead. You might enjoy it more.

speedofdarkness 

StarCraft: Speed of Darkness

Usually I avoid books of a series. Except maybe Star-trek, cause I know the characters well, and love to read about them. I’ve played StarCraft for a couple of weeks years ago, and enjoyed it. I know some of the back story behind StarCraft; enough to not find it mindblowing. I picked this book up on a whim, and because it was cheap at about 25 cents (that’s what I paid).

But this one packs a surprise punch. Strong enough to make me look at the cover again so that I could read the author’s name, and then go and Google him up. Tracy Hickman is better known for fantasy, but in this book he has written a superb story that captures the spirit of Starcraft too well. The story is of a small tme private in the confederation. Too insignificant to be of value; his brain re-programmed again and again like a machine until his real and fake memories meld so that he knows not which are which.

He is put to battle against terrible odds, and deserted by his own comrades. He and his comrades battle odds and tragedies, and in spite of being betrayed by their own, they sacrifice themselves to save the innocents.

This a gallant book by Tracy Hickman, written wonderfully through the perspective of one soldier to tell the story of every soldier in the confederation army. This book would have been wonderful whether it had the StarCraft name or not. It’s beautiful, brave, funny and tragic. It’s everything a war novel should be, and yet it’s so small, hardly more than a novella.

If you find this book you would enjoy reading it.

Is a Ban on Smoking on TV fascism?

Film actor Rahul Bose thinks so, and lending a supporting voice is the former VJ who-never-made-it-as-an-actor Gaurav (I don’t know what his full name is). That’s what Rahul Bose said to A. Ramadoss, India’s health minister who’s pushing for a ban on advertising, and showing of alcohol, smoking, and junk-food (where did that come from?) on TV and film.

Rahul and Gaurav argued fiercely for allowing free reigns to cinema industry to show all the smoking and drinking they want. Teenagers, don’t want to be told what not to do,’ Gaurav said. ‘And anyway,  if cinema had any negative impact on young people, you’d have people committing all sorts of heinous crimes,’ quipped in Rahul. ‘It’s naive to think that cinema can influence young people to light up,’ they both sang in chorus.

Who’s being naive here Rahul, Gaurav? Definitely not you guys. You’re smart folks, busy protecting your pay packets; influence on youth go to hell. How can you guys say to camera that watching Shahrukh Khan in a film lighting up does not influence anyone to light up. Does that mean that those millions of bucks that Cola companies spend each year to get Shahrukh to drink Pespi on camera is a waste? Brand-ambassadors are useless? Or maybe this is some sort of selective magical influence that works when Shahrukh picks a Pepsi, but fails when he lights up.

killtobacco

Rahul Bose and Gaurav you are shameless. Specially you Gaurav. You picked up smoking for ‘no-reason’ at the age 21, to discover if it gave you some sort of connection. Well at 21 you weren’t smart enough to figure out smoking is harmful and it took you 5 more years to get that message, and you who’ve suffered from the scourge of smoking should side with its spread?

Rahul Bose you were seen picking up a drink in your movie Shaurya every five minutes. Except one scene where did it help take the movie forward? Granted there’s a very prevalent scotch culture among army-officers; but the story could be told with nothing missing without you going for a drink so frequently.

Gaurav and Rahul the health minister may have many agendas (including getting control over AIIMS), but in this one there’s no public evil. Do you say that it would not help the society if there were fewer alcoholics around, and less people died of lung ailments?

You, and those who supported you, including that sorry looking vice-president of alcohol producer’s association, and that I’ll-peddle-any-crap president of a prominent ad agency are just trying to save your job; like the rest of us.

p.s. The sensible ones amongst us have decided not to buy anything produced by companies that produce tobacco products. This includes the entire range of sunfeast biscuits, Fama soaps, Bingo chips, produced by ITC- Indian Tobacco Company, or I’am Toxic and Choking.

Is the future Esperanto?

Esperanto does not belong to any culture, region, or community. It’s not been developed over generations. It’s a language created to be most suitable for international communication. A language which was to be the language of the world. Esperanto has been talked about a lot in science fiction. After all, science imagines a time of united humanity when the entire world speaks one language.

I do believe that the time of one universal language that all humanity will know and communicate in will come, but I suspect it’s not going to be Esperanto. In spite of all its phonetic flaws, grammatical quirks, and syntactical oddities, the strongest candidate for this position is English. English which will be far removed from its root. Virtually unrecognizable in the new form that the speaker of the new tongue will never suspect it’s the same language. I believe that English will evolve, mutate and distort under the pressure of other languages like Hindi, Mandarin, French, Spanish, and it will become the ONE language that the world will use.

The simple reason is that English already has a headstart that no other language enjoys. It’s already the language of international business, political communication, and now the Internet. More documents are made in English than any other language. So why is there a reason to doubt the potential?

Esperanto must be respected as a concept, a powerful idea. But it is foolhardy to believe that any language can REPLACE the most widely used language in the world.

Languages are easy to learn for children, and as the children of the next generation grow up, they will communicate internationally with people of many regions in English, and they will evolve it by introducing their own words and concepts.

The future one language of man won’t be Esperanto, but the English that we will speak in the future will not be the English WE speak.

Languages evolve very little by the force of government as was projected in 1984, the book. They evolve very fast by the will of the people. The people are voting for English all over the world.

Maybe we can redub English Esperanto. :) All for the respect of the concept.

Microsoft’s Expression 2 is out

I must have been one of the first few people around to lay their hand on Expression, and tinker with upcoming technologies like Silverlight and Blend. Later I decided that Silverlight wasn’t doing anything productive for me and I went back to plain .Net. Let Microsoft ready Silverlight 2 before I can hope to do anything useful in it.

Meanwhile Microsoft has readied Expression’s new version. Expression 2. This is a great suite for a multimedia developer. You’ll find a great designing app, an animation tool, media formats tool, and a web designing tool.

Microsoft is giving a free trial. You can find it on http://www.microsoft.com/expression/

What Knuth said

knuth Donald Knuth is the old guy of programming, and his books on algorithms are like bibles to the programmers who are smart enough to comprehend them (don’t look at me, I am not one of them). I read a recent interview of his in which he expresses his views on unit testing, mocks, extreme programming, etc. When I started the interview I thought it would be some great advice from the great maestro. Something that would motivate me to work harder into incorporating these techniques in my own programs. But as I read it, I read with increasing glee and by the end of it I was feeling considerably less guilty about not mocking and unit testing. Donald Knuth only has scorn for them.

But I do feel a little doubt somewhere that no matter what Knuth said, someday, and that day might come soon, I will have to start mocking and unit testing.

All in the name of good programming practices.

Why don’t I write on programming any longer?

Before my blog was destroyed, I wrote exclusively on programming, but ever since I started this new blog I haven’t been able to write much on programming. It’s not that I don’t have anything to share, or no desire to share, they’re both there. Yet, whenever I get so bored just by the thought of writing on programming that I can’t bear to look at the PC.

Instead I would rather write this introspective piece of crap.

This is how the things stand, and this is how they will be until they change. Ha ha!

FOB: Space Soldiers

ss A couple of days ago I was trying to add paragraphs to a story that I’ve been writing for more than a year now. It’s still got just 7 pages. I was writing about war. War is humanity’s greatest innovation. No other species has brought genocide to such organised levels. Think about it, bringing together several thousand individuals and attacking several thousand other individuals, not for food, not for land, for what then? Lions won’t do that, neither would moose.

Space Soldiers is about a future that hasn’t learned to stop warring. There are tough soldiers, old generals and grizzly villains. So stereo-typical, so ho-hum, only if it wasn’t for the special worlds that the authors have concocted. These wars are on different planets, on different species, and are fought in a way you’d never imagine.

Actually I’ve got it all wrong. The book has it right. It’s not about wars, it’s about soldiers. Soldiers who’re paid to kill, and die if needed. What they feel, and what they do. Joe Haldemann wrote the forever war after he got out of Vietnam. That was probably the only space-war story I really-really liked. There are some pretty likeable ones in this collection too, but not the ones that you’d really-really like.

War psyches us. It plays at inner fears. War is mass-reaction to hidden fears. War is like god in a way, creation of hidden desires, fear and inadequacies.

No more digressing. About this book. It’s good, some of the stories are really exciting. I liked the ones the critics didn’t like, and didn’t like the ones the critics liked. The book must be good if the critics got it all wrong.

You wanna read it? Nyah, you can’t find it. It’s not available so easily in India. I got it from a pavement seller for 20 Rs. It’s not going to be in a bookstore near you. Sorry :{p