views:

250

answers:

3

I am porting an existing application to a DotNetNuke module and discovered bizarre behavior. I use ClientID when creating javascript so that the code can identify the HTML elements. Normally that creates a value something like this:

"g_ctl00_ctl01_ctl00_ctl00_txtSearch"

We've all seen this a million times, right? Well, after porting this code to a DotNetNuke module and running it for the first time, the ClientID property is returning this:

"dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl00_ctl00_txtSearch"

Notice the comma and the space. This is causing all kinds of javascript errors. For example, the ASP.NET Login control is now outputting invalid javascript:

var dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired = document.all ? document.all["dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired"] : document.getElementById("dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired");

My module has an assembly name of TechInfoSystems.GalleryServerPro.dll, the default namespace is GalleryServerPro.Web, and the user control is in a class named Gallery.cs, so that partially explains where some of that extra text is coming from, but why is it there? And what can I do to ensure that ClientID does not output commas, spaces, or other characters that can cause problems in javascript?

Thanks, Roger

A: 

I have NO idea why this is happening (but then I've never used DNN)

what you could do is take control of the ClientID yourself for the affected servercontrols, for instance, it looks like you're using a TextBox control with server ID = txtSearch, what you could do is make a class that inherits from TextBox and setting its ClientId to be equal to the Server ID (assuming there's only one control called txtSearch) see here

It might be that DNN would override even that, but it might be worth a shot

AndreasKnudsen
I had initially tried to override UniqueID (which is used when generating ClientID), but that caused other problems such as click events no longer firing. In the end the answer was to assign an ID to the user control.
+1  A: 

I (sort of) figured it out. I needed to add an ID to the user control that is being added to the DotNetNuke page. This is the main view user control - the one that inherits PortalModuleBase. The user control is contained entirely in a code-behind class (no .ascx), so adding this to the Init event fixed everything:

this.ID = "gsp";

My theory is that the issue happens when these are true:

  1. The "view" user control uses only the code-behind file (no .ascx file)

  2. The user control does not have an ID specified.

When registering a module without the .ascx, one must specify the class and assembly in the Module Definitions. In my case, it is "GalleryServerPro.Web.Gallery, TechInfoSystems.GalleryServerPro". That looks very close to the text DNN ends up inserting into the clientID string. DNN (or ASP.NET) must be taking that string and using it to build the clientID in the absence of an ID assigned in the user control.

Not sure if this is a DNN bug, but I am happy to figure it out and now can move on...

A: 

You could try referencing your control using a class instead? There are plenty of good functions out there for doing such a thing. I would recommend jquery for a start, to make all things javascript a world easier.

Ben Cull