views:

746

answers:

2

i need to add JavaScript calls in some controls attributes, i am using master pages but in order to obtain the contentplaceholder client id i am iterating over the forms controls. is there another way to obtain in the server side code of the content page?

 foreach (Control control in this.Form.Controls)
                {
                    if (control is ContentPlaceHolder)
                    {
                        contentPlaceHolderID = control.ClientID;
                        break;
                    }
                }
+1  A: 

I do the following in jQuery:

$("*[id$='DivFullWarning']").each(function() {
  // do whatever in here
});

this searches for any control that contains DivFullWarning in their ID, so you could name your ContentPlaceHolder "contentPlaceHolder" and then no matter how much asp.net muck's with your id, you'll still be able to grab it client side.

You could just expose the content placeHolder somewhere so you don't have to loop through all the controls.

sontek
A quick thought, wouldnt that also match all automatically generated ids inside the ContentPlaceHolder?
Simon Svensson
hah, thats a good point =)
sontek
+2  A: 

If there's only few controls like this you can set up a global variable:

var contentPlaceholderId = '<%= this.ContentPlaceholder1.ClientId %>'
Rashack