+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Problem with auto generated control ids

Problem with auto generated control ids

12 posts, 0 answered
  1. DamienR
    DamienR avatar
    2 posts
    Registered:
    27 May 2008
    09 Dec 2008
    Link to this post
    Hi,

    We have been using sitefinity for a few projects and we find that most controls seem to always assign controlname_1 as the id when dragged on the page in the admin. Obviously this causes problems when multiple controls of that type are used as it doesn't seem to increment the suffix on id.

    I have found that giving the control a static control id prefix eg "ControlName_" and then appending a timestamp is a great solution but something I would rather not have to do for each control.

    Any thoughts on this?

    Cheers,
    Damien.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    09 Dec 2008
    Link to this post
    Hi DamienR,

    To prevent naming conflicts when multiple instances of a control are rendered, ASP.NET automatically generates   IDs value for each control on a page. If the ID of the control is not specified, an automatically generated value is used. Each part of the generated ID is separated by an underscore character (_). The IDs could be edited manually if there are not so many controls.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. mimholt
    mimholt avatar
    11 posts
    Registered:
    01 Mar 2007
    20 Dec 2008
    Link to this post
    Any solution for when there are many controls on the same page?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    22 Dec 2008
    Link to this post
    Hi mimholt,

    You can try specifying the control ID as below:
    <%# ServerButton.ClientID %> " 

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. mimholt
    mimholt avatar
    11 posts
    Registered:
    01 Mar 2007
    01 Jan 2009
    Link to this post

    Hi Ivan - 

        I'm not sure exactly what you mean.  If I have multiple instances of the same user control, it's the ID of the user control that gets repeated . . .not of any of the child controls. 

    Can you give me an example?

    Thanks -

    Matt
  6. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    02 Jan 2009
    Link to this post
    Hey Matt,

    I believe unique ID's should be generated for each control you drag & drop onto your page.  This happens automatically and I've never worried about it.

    For example, I drop a new Generic Content control onto my page and the ID is GenericContent1.  Then I drop another Generic Content control onto my page.  The ID for this control is GenericContent2.

    I just tested this with Sitefinity 3.5 and it works fine. 

    Are you seeing different results?  What control are you using?  What version of Sitefinity are you using?

    Gabe Sumner
    http://www.sitefinitywatch.com/
  7. mimholt
    mimholt avatar
    11 posts
    Registered:
    01 Mar 2007
    02 Jan 2009
    Link to this post
    Hey Gabe - 

        I'm using the latest release (3.5 SP1).  And, you're correct - custom controls like the Generic content control (and some that I've written) do behave as expected.  It's user controls that don't.

      If you create a folder under the sitefinity project called "UserControls" and add an ASCX file to it, every compiles and you can add the control to a page - just as explained in the developer guide.  If, however, you add a second instance of the user control to the same page - boom!  they both get the same control ID.  For example, if you build a user Control called "MyUserControl" the first instance gets an ID of "MyUserControl1" and the second should get an ID of "MyUserControl2" . . but it seems to get "MyUserControl1".  Does that explain it?

    -Matt
  8. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    02 Jan 2009
    Link to this post
    Thanks for the additional explanation.  You are correct, the ID naming behavior seems to be different for custom usercontrols.

    Here is what I did:

    I created a very basic custom UserControl.  I dragged the first instance of the control onto my page.  The ID was set to usercontrols_customcontrol_ascx1.

    I then dragged a 2nd control onto my page.  The ID was blank.  I also dragged a 3rd & 4th instance of the control onto my page.  All subsequent control IDs were blank.

    I saved my Sitefinity page and then re-edited the page.  All custom control ID's were now blank.  Even my original instance of the control.

    I'm not sure if this is the intend behavior or not.  I did not experience any problems from having blank control ID's.  I tossed some TextBoxes into my custom control to see what ID's these controls would inherit.  Here is what I saw:

    <div><input name="ctl00$Page_Content$ctl00$TextBox" type="text" value="Hello World" id="ctl00_Page_Content_ctl00_TextBox" /></div
    <div><input name="ctl00$Page_Content$ctl01$TextBox" type="text" value="Hello World" id="ctl00_Page_Content_ctl01_TextBox" /></div
    <div><input name="ctl00$Page_Content$ctl02$TextBox" type="text" value="Hello World" id="ctl00_Page_Content_ctl02_TextBox" /></div

    Those are unique ID's.  They aren't visible in Sitefinity but ASP.NET to handling this at runtime.

    Again, I'm not sure this is working properly, but I did not experience any direct problems due to this behavior.

    Gabe Sumner
    http://www.sitefinitywatch.com/
  9. mimholt
    mimholt avatar
    11 posts
    Registered:
    01 Mar 2007
    03 Jan 2009
    Link to this post
    Hey Gabe - 

        When I perform the same test, I get a slightly different set of results, but I think I know why.  When I do the same test, all of my user controls get an ID of usercontrols_customcontrol_ascx1.   And if I click on the "preview" button (or publish it outright) and then run the page, I get an error about the key already being in the dictionary.  (Sorry, I didn't grab the exact error message).

    And then I realized I did something  . . .  When I opened the project (inside of a solution containing some LOB dlls), Visual Studio asked me if I wanted to upgrade the site to 3.5 . . . and I didn't think and said "Yes".   I'm sure that the slight differences in behavior have to do with the differences in the .NET runtime. 

    Honestly, I'm not going to worry about it too much.  Most of my controls have fairly simple UI's, so changing them into custom controls is easy.  I did want to post the issue, just in case it's a problem with the next release of sitefinity (which I think is supposed to include 3.5 support).

    -Matt
  10. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    06 Jan 2009
    Link to this post
    Hello,

    What really matters is what the controls renders as an html. All controls with runat="server" attribute will be generated with unique IDs in the markup.
    I was able to drag two controls on the same page, and saw that they were with the same control ID. Still I did not get any error while saving/previewing the page. Then I remembered about the error you got - key already being in the dictionary. In a normal aspx page, if you have two controls with the same ID, you will get "A control with the same name already exist" instead of duplicating key in a dictionary. This make me believe that the problem might be somewhere else.
    Could you please send us your Sitefinity error log file, right after you get the error message? It is located in App_Data directory.

    We will get back to you as soon as we have a suggestion that might be helpful.

    Best wishes,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  11. Jan
    Jan avatar
    39 posts
    Registered:
    22 Jan 2010
    04 Mar 2011
    Link to this post
    Hi

    I have a problem that have origins in this problem.
    My control gets data from an external source. We have templates and the generated output takes some time to process. So, I've created an internal cache for just this control.

    What I do is that I store the generated html output into a Cache[CacheKey]
    The CacheKey is set as Request.Url.AbsoluteUri + "_" + this.ClientID

    When I drop multiple controls to the same page, they get the same ID and the generated output for control 1 is displayed in control 2. So it's not a matter of the control just renders as html or not.

    I Could use something diffrent for cachekey, even put a seperate propery for the user to set, but it would be nice if this worked "out of the box"
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    04 Mar 2011
    Link to this post
    Hello,

    There is an issue with the control ids if you use RadAjaxPanel. The bug ID is 106455. We found the that issue does not occur if you use the ASP.NET UpdatePanel.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Register for webinar
12 posts, 0 answered