CoComment Script for Community Server Update

It’s the little things that keep you on your toes as a programmer.  After my site upgrade to Community Server 2.0, Keyvan ever so politely pointed out that my CoComment script wasn’t working, i.e. coco enabled visitors weren’t having their comments automatically logged on the coco service.  After a few minutes of digging around, I realized it was because in my original CS coco script I had hardcoded the commentFormName variable to always be __aspnetForm.  Lo and behold my form name is different after the upgrade, so obviously hardcoding this value isn’t a good idea.  Yeah, magic string syndrome.

The problem with this is that due to either A) The way CS2.0 skins name forms, or B) The way the asp.net runtime works when generating form names (don’t know which of the the two is the guilty party), the form name was no longer __aspnetForm and had become aspnetForm.  Apparently this isn’t a constant, so back to the drawing board to get this value programatically.  Unfortunately due to either A) The way CS2.0 skins name forms or B) How asp.net 2.0 generates form names (again, I have no idea who the guilty party is) this wasn’t as easy as simply changing the commentFormName variable declaration to Page.ID/UniqueID/ClientID (all came back as an empty string), and CS refused to compile when attempting to use Page.Parent.<whateverID>, coughing up a null reference exception. 

What’s interesting is that Bob said there’s a Form member on the page class (which I didn’t see in the MSDN docs), which in turn has a Name property, so I gave that a shot and was promptly met with an accessor level denied error message (compiled under asp.net 1.1 but running on 2.0)…reflecting on System.Web.UI.Page revealed that there indeed was not a Form member on Page (as expected), so why that exception instead of a “member doesn’t exist” or whatever?  Strange stuff, but after reflecting the same class on the asp.net 2.0 version of the assembly there is now a Page.Form member, and of course Form has a Name property which would have nipped all of this in the bud.  So what’s the fix?  Either recompile CS2.0 under asp.net 2.0 and use the afore mentioned solution, or simply change your comment form variable to the following javascript:

commentFormName = document.forms[0].name;

Yeah, it’s that easy (or difficult if you factor in how much time I spent digging through .Net documentation trying to figure out how to do this from server side code).

Comments

# Dave Burke said:

We CoCommenting CSBloggers appreciate that digging time you put into this.  Thanks!

Sunday, February 26, 2006 12:49 AM
# Keyvan Nayyeri said:

But for me with ASP.NET 1.1, the form name was __aspnetForm and my previous script worked properly.

This is so easy when you go to do this on Mobile webforms on ASP.NET 2.0.

Sunday, February 26, 2006 5:40 AM
# Jayson Knight said:

Yeah, I plan on recompiling my site under 2.0 soon.  That worked b/c from I can tell, unless you specifically named your top level form something explicitely from code, the asp.net 1.1 runtime named it __aspnetForm so hardcoding this value in the coco script was fine.  This isn't the case in 2.0; after doing some testing I've seen various different names assigned by the asp.net 2.0 runtime, so it's a better idea just to snag the form name via some sort of code.  Personally I'd rather do it from server side code than javascript which is why I was so surprised that there was no easy way to do this asp.net 1.1.

Sunday, February 26, 2006 6:48 AM
# John Hamman said:

Hey Jayson, I have compiled the source with vs2005 and have had no problems. I am looking to convert the current masterpages to asp.net2. masterpages. would you be interested in that?

Tuesday, February 28, 2006 2:15 AM
# Jayson Knight said:

Hey John, just got done w/ my recompilation as well, JK.com feels even snappier now.  I am most definitely interested in what you mentioned, but the only caveat (if you could even call it that) is that CS3.0 is slated to do this as well.  Granted sooner is better than later, so yeah, let's get it on!

Tuesday, February 28, 2006 9:46 AM
# John Hamman said:

I am getting a few errors on my compile, that I only see and find in the exceptions report. Check yours and tell me what you get. Email me (my full name with a period between at gmail, like first.last@gmail.com) and lets swap notes. I have been touching the code that was pushing warnings on the compile so that might be where the exceptions are comeing from.

Wednesday, March 1, 2006 12:09 AM
# Douglas Rohm said:

Thanks for all the great information on how to do this Jayson!

Thursday, March 2, 2006 6:31 AM