Find Element With JavaScript When UserControl Used Multiple Times

Posted on 2013-11-19
Last Modified: 2013-11-19
Maybe I am making this more complicated than it is but for the life of me I can not find any information on it.  Basically what I want to do is place a hidden element on a user control and write JavaScript that will read that value.  The problem I have is that it needs to be able to work when there is more than one user control present on the page.

I know that I can not place the JavaScript code on the user control itself because it should only be added to the page once.  I currently have all of my JavaScript in a file and I add it to the page using Page.ClientScript.RegisterClientScriptInclude.  So the question is how do I write JavaScript that can be placed in the file and read the value of a hidden element on the user control that is calling the function and make sure I am reading the right control.

Maybe there is another way to accomplish what I am trying to do so I will try to give this a little more context.  The user control I am creating contains a RadToolBar that has buttons the user will press to perform various actions.  One of the buttons will perform a delete operation so I want to confirm the user really wants to delete it.  My current plan is to use RadWindow to display a confirmation message.  The code that I have found to do this is:

            var btn; 
            function onToolBarClientButtonClicking(sender, args) { 
                var button = args.get_item(); 
                if (button.get_commandName() == "DeleteSelected") { 
                    if (document.getElementById('hdn_Hosp').value) { 
                        btn = args.get_item(); 
                        confirmFn('Delete ' + document.getElementById('hdn_Hosp').value + '?'); 
            function confirmFn(text) { 
                var callBackFn = function (arg) { 
                    if (arg) { 
                        document.getElementById('hdn_Hosp').value = ""; 
                radconfirm(text, callBackFn); 

Open in new window

I think this code could be written so that it doesn't use a hidden element.  My second issue though is that I need to get a translated message to display.  I already have the translations server side so my plan was to put the translated text in a hidden element that could be read and displayed.
Question by:kmcbrearty
1 Comment

Accepted Solution

kmcbrearty earned 0 total points
ID: 39659997
I was able to come up with a solution that works with .Net 4.0.  Basically with .Net 4.0 you can specify the ClientIDMode.  More information on ClientIDMode can be found at the Microsoft Developer Network website.  The default value of ClientIDMode is Predictable which means that it will concatenate ClientID values for each control in the hierarchy separating each ID with an underscore to create a unique id.  So for example you might have an ID of:


With some string manipulation we can determine the ID for the control that we want from the ID of the calling control.  The basic idea is to strip off the known control name, the name of the control that called the function, as well as an extra character to account for the underscore.  This gives you the base name for all of the controls that are at the same level in the control hierarchy.  You can then combine the base name and the name of the control that you are looking for separating them with an underscore to get the name of the control you need to read.

The full solution that I came up with is below.  For the getUserControlName function I know that I could simplify the function by just specifying the number of characters to strip off, the length of the toolbar name plus one for the underscore, but I believe the extra lines improve the readability of the code and make it easier to understand how it works.  It is my belief that fewer lines of code doesn't always mean better if it isn't easy to understand the meaning or how it works.

var button;
var showConfirmation = "y";

function getControlName(sender, name) {
	var userControlName = getUserControlName(sender);
	return userControlName + "_" + name;

function getUserControlName(sender) {
	var senderId = sender.get_id();
	var toolbarName = "ActionBar";
	var nameLength = senderId.length - toolbarName.length - 1;
	return senderId.substr(0, nameLength);

function getConfirmDeletePrompt(sender) {
	var controlName = getControlName(sender, "ConfirmDeletePrompt");
	return document.getElementById(controlName).value;

function getConfirmDeletePromptTitle(sender) {
	var controlName = getControlName(sender, "ConfirmDeletePromptTitle");
	return document.getElementById(controlName).value;

function onToolBarClientButtonClicking(sender, args) {
	button = args.get_item();

	if (button.get_value() == "delete")
		showDeleteConfirmation(sender, args);

function showDeleteConfirmation(sender, args) {
	if (showConfirmation == "n") {
		showConfirmation = "y";
	var title = getConfirmDeletePromptTitle(sender);
	var text = getConfirmDeletePrompt(sender);
	radconfirm(text, confirmCallBack, 300, 10, null, title);

function confirmCallBack(arg) {
	if (!arg)

	showConfirmation = "n";;

Open in new window


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question