AJAX-Enabled Web-Parts: solving the problem of changing Client IDs

AJAX-Enabled Web-Parts: solving the problem of changing Client IDs One of the most challenging aspects of building SharePoint-enabled web parts is using JavaScript to interact with specific controls. As a consequence of how SharePoint renders controls with id’s in Sharepoint WebParts, it’s near impossible to write clean JavaScript. From a maintenance perspective I HATE using server-side code to render JavaScript. It’s inelegant and reeks of a lack of forethought. Yuck yuck yuck. It’s like sweeping dust under the rug. Wherever possible, webpart-specific javascript should be held within a single .js file (stored in the 12Hive). The problem is that Sharepoint alters a control’s ID during the page rendering process. So, a label with the ID “label1” might become “ctl00_label1”. The naming process is entirely unpredictable. This makes interacting with a specific DIV, INPUT or any other html element very difficult because you can’t predict the rendered IDs during development. This is especially problematic if you’re looking to host several instances of a webpart on a single page. One really terrible way of dealing with this is to render the html yourself by adding literal controls ala

LiteralControl l = new LiteralControl();
l.text = '
'; this.Controls.Add(l);

This is a terrible approach which is severely limiting and unnecessary. To tackle the problem I’ve devised the following solution which revolves around creating a Javascript class to accompany the web part:

  • create properties to store the dynamically generated IDs of every html component to wish to interact with
  • create pointers to every html component you wish to interact with
  • create an init function. As input you pass the clientID of the webpart AND the id’s of every component you wish to interact with.
  • this function will store the id’s using the properties
  • this function will also attach the pointers to their respective objects
  • this function should also attach any eventhandlers
  • create any supporting functions (i.e. webservice calls, webservice receivers)

This approach embraces the absence of reliable identification info. It is also sympathetic to having multiple copies of a webpart on the same page. Here’s what the javascript might look like

function UpdatingLabelWebPart() 

	var me = this;	//required to reference the object at runtime  
	this.clientID = null;	//ids 
	this.labelID = null; 
	this.buttonID = null;
	this.label = null;	//controls 
	this.button = null;
	this.register = function (clientID, labelID, buttonID) 
		me.clientID = clientID; //init the id 
		me.labelID = labelID;
		me.buttonID = buttonID;
	this.attachToButton = function() 
		//if the controls haven't loaded yet 
		if (document.getElementById(me.buttonID) == null || document.getElementById(me.labelID) == null) 
			setTimeout(me.attachToButton, 50);
			//register the controls 
			me.label = document.getElementById(me.labelID);
			me.button = document.getElementById(me.buttonID);
			//attach any events button.AttachEvent("click", me.buttonClicked);
	this.buttonClicked = function(sender, args) 
		me.label.innerHTML = "Hello World";

This approach lets you have multiple instances of the same AJAX-enabled webpart and saves you the hassle of having to write annoying javascript (having to write me.label is hardly a chore).

By admin on September 16, 2008 | Ajax, ASP.NET, c#, Mac, Sharepoint | A comment?

Swiching to Mac? Software you need

Chris Thomson has posted a fairly extensive list of tools for people moving to Macs. 

Credit to Ross Mayfield for finding the list.

By admin on December 31, 2007 | Mac | A comment?

Confessions from a closet Mac admirer

For a while I have been coveting my neighbour’s wife.  I have longed for his flashy car.  I have been stealing glimpses of his well manicured lawn over the fence between our two houses.

I wants his Mac.

It all started two years ago.  I had just joined Linklaters, and was working with Matthew Parsons.

Matthew loves Macs.

Matthew promised one was in my future and that I would own on within 12-18 months.  Ha!

He showed me Keynote, and the presentations he had put together using it.  “Try doing that in Powerpoint!” he said.

I stared down at my shoes and refused to look him in the eye.

When doing internal presentations, I began to insist upon using Keynote to put the slides together.  We had to revert to pdf so the slides could be shared, but the loss of the animations was acceptable.

Still, I did not buy one.

I borrowed his Powerbook to put together something for my football team.  The process was both frustrating and enlightening.  The annoying things that didn’t work the way I wanted were down to habits built up over many years.  For example, while the Powerbook only has one button, you can get the “right-click” experience by drumming two fingers on the keyboard.

Different, but simple.

One year passed, and nary a Mac to be seen in the house of Neil.  Matthew threatened to buy me a Mac instead of giving me bonus.  I was indignant.

Still, my resolve was weakening.

The walls came tumbling down on June 12, 2007.  I downloaded the Jobs keynote address from the World Wide Developer Conference.  Love or hate Macs, the man is a genius when it comes to presenting.

Came for the presentation, stayed for the Mac.  Leopard blew my socks of.  I drooled all over myself.

Serious, SERIOUS computer envy.

Later that month I flew back to Vancouver to visit my mother who was ill.  She has complained about her Windows computer for years, and now I had the answer.  We discussed the idea of getting her a shiny new iMac once they were released and she was sold.

I came back to Vancouver in August for a scheduled holiday and bought a beautiful new 20″ iMac.  Taking that machine out of the box was a 2001 Space Odyssey moment. 
Mom doesn’t care for technology at all, but she liked it. 

The next few days I showed her how to use it, and she began to gloat to my father, a Windows and Linux man, about her new computer.  He subsequently tried to sabbotage it by installing Open Office, but mom figured out how to download and install iWork and is using Pages instead. 

Months later and she is still happy about her computer (“I can listen to the BBC on it!”).  It just works.

Since August, I’ve been waiting patiently.  First, I waited for the release of Leopard.  Fan or not, upgrading an OS when I could save myself the hassle is a no-brainer. 

Now I’m just building up the courage to part with a hefty chunk of change.  Mac Powerbooks are expensive, but I don’t think I can wait any more.  

By admin on December 28, 2007 | Mac, Uncategorized | A comment?