Custom web part with editable regions – how to ensure unique IDs of nested editable regions
If you want to develop a custom web part which includes an editable text or editable image you can meet the problem that IDs of editable regions must be unique. If you place the custom web part several times on your page the IDs of nested controls are the same.
This article will help you to develop a custom web part with unique dynamic IDs of editable regions.
You can follow the instructions for developing new web parts in our
Developer’s Guide.
We will place two editable text controls and one editable image to our sample web part.
The code could look like:
<%@ Register Src="/CMSWebParts/Text/editabletext.ascx" TagName="editabletext"
TagPrefix="uc1" %>
<%@ Register Src="/CMSWebParts/Text/editableimage.ascx" TagName="editableimage"
TagPrefix="uc1" %>
<div>
<uc1:editabletext ID="txtTitle" runat="server" RegionType="TextBox" DialogHeight="20" DialogWidth="250" RegionTitle="Title" />
<uc1:editableimage runat="server" ID="imgUpload" />
<uc1:editabletext ID="txtDescription" runat="server" RegionType="htmlEditor" DialogHeight="100" DialogWidth="250" RegionTitle="Description" />
</div>
We will assign the unique dynamic IDs in the code behind. We will use Page_Init method for this purpose.
protected void Page_Init(object sender, EventArgs e)
{
this.imgUpload.ID = "imgUpload_" + this.ID;
this.txtTitle.ID = "txtTitle_" + this.ID;
this.txtDescription.ID = "txtDescription_" + this.ID;
}
It uses the composition of current control ID which is always unique on the page and the custom ID.
-hg-
See also: Applies to: Kentico CMS 5.0