Creating custom controls in Sitefinity is a great tool for .Net developers; however, there are issues that can occur when using more than one of the same custom control on a single page or template. Avoiding duplicate IDs with custom controls is key to keeping a page, and its controls, error-free and usable.
One issue that occurs when controls have duplicate IDs, or no IDs assigned, is when using the RadEditor within a created them. If you’d like to see this issue occur, download the following files, add them to your web.config into the toolbox, and try using more than one of them on a page or template.
Within this control, you will see that there is a Generic Content area that will save as text within it, as well as a Properties tab that includes a file upload, and link. Once you’ve added 2 controls to the same page, add some text to the generic content RadEditor area, and save. Once you go back to try and edit the controls, you will not be able to change anything within the Properties tab, without first removing any content that is within the Generic Content area. This is an issue with duplicate or missing IDs on the controls themselves.
To fix the duplicate IDs in Sitefinity controls, there is a simple fix to the code-behind. I’ll be showing this in C#, but can easily be converted to VB.Net if needed.
Within the control needing an ID (and this is also a good practice for ANY control you will be creating!), add the following on the Page_Load event:
ID = Page.ClientID + DateTime.Now.Ticks;
By doing this, it will nearly guarantee that every single control will have a unique ID, thus eliminating any errors or problems when editing pages or templates in Sitefinity.
Simple tip – to further decrease the ID name length, yet still ensure a unique ID, you can remove the ClientID.
ID = DateTime.Now.Ticks;