[RESOLVED]Can't find control inside a detailsview

Hi all,

Background:

Site that uses master pages

In a content page I have a detailsview control containing many controls – textboxes, labels, dropdowns etc.

Detailsview has Template fields with item and edititem templates

I want to access the value of a label within an itemtemplate field.

Outline:

"Head" ContentPlaceholder 
    "Main" ContentPlaceholder 
         Bootstrap Row
           .... 
         End Bootstrap Row 

         Bootstrap Row 
           <div> 
              DetailsView 
                 Fields 
                    TemplateField 
                       ItemTemplate 
                           Label ID="registrationID" -- THIS IS CONTROL THAT CONTAINS THE VALUE I WANT 
                       End ItemTemplate 
                       EditItemTemplate 
                           Textbox 
                       End EditItemTemplate
                         . . . . 
         [END TAGS]

In Javascript

var regIdText = $('#' + '<%= registrationID.ClientID %>').val();

But I get an error saying registrationID is not declared.

I tried adding a css class to the label and selecting it that way but it failed as well.

Any ideas?

Becuase the registrationID control is nested within the template you won’t be able to access it outside that ItemTemplate.

What you can try is on that Label control add the attribute ClientIDMode="Static".  Then in your javascript change:

var regIdText = $('#' + '<%= registrationID.ClientID %>').val();

To

var regIdText = $('#registrationID').val();

Hi SeeDosRun,

Thanks for your post.

ASP.NET allows certain controls to be denoted as naming containers. A naming container serves as a new ID namespace. Any server controls that appear within the naming container have their rendered id value prefixed with the ID of the naming container control.
In this situation, you should visit the page through a browser and view the HTML source to check the id of the Label then get the value according to correct id.

For more information about “Control ID Naming in Content Pages”, you could refer to the below link.

Hope it will be helpful to you.

Best Regards,

Fei Han

Thanks for you suggestions.

I ended up using

$("[id$='registrationID']").text();

that way I don’t have to worry about naming containers.

Thanks again.

Leave a Reply