views:

43

answers:

4

Suppose I have a master page where I have written some javascript to access the value of a particular asp.net text box, which resides in one of its content pages, but not in all the pages. This piece of javascript code can't be moved to that particular content page because of some restrictions.

Now I need a way to determine whether or not that text box exists in the page, which will imply that the content page containing that text box has been loaded.

How I can I do that in javascript?

+2  A: 
var productElement = document.getElementById("<%= YourTextBoxID.ClientID%>");
if (productElement != null)
{
// Code here when the Element Exists.
}

Hope that helps!

EDIT (after you clarification):

In your master page:

<% if (FindControl("YourTextBoxID") != null) {
     //do smthng, you can add some jscript that should be executed, or something else you need
} %>
anthares
Nope, it does not work. When I run the site's master page, an error message is shown, which says - The name 'YourTextBox' does not exist in the current context
Night Shade
Ahh ... I see... Well then you should use with server tags the following statement:<% if (FindControl("YourTextBoxID") != null) { //do smthng} %>I think there is no way to do it with only javascript, because you don't know what ID it is supposed to have.
anthares
+1  A: 
var elem = document.getElementById('myElement');
if(elem)
{
    // Do stuff when elem exists
}

Anything that is undefined or null (in this case the element that doesn't exist) will equivocate to false. So, if it exists, it becomes true, and the if statement will run.

Jeff Rupert
I can't access an asp.net control using this approach. Because when the control is processed in the server, it assigns a new and unique id to its equivalent html text box, which can only be accessed by using ('<%=myElement.ClientID%>')......
Night Shade
Can you create an `<input type="hidden"/>` element somewhere in your content page that you're POSITIVE of you can know the id, then set its value to `<%=myElement.ClientID%>` ? Or, if you use jQuery, you can simply use the generic page structure to narrow down what you want so you can get that ID value. I don't know anything about ASP, so I didn't realize that it had awkward functionality like that. Sorry my answer wasn't that helpful... =/
Jeff Rupert
+1  A: 

On client side, to access control, you need to use ClientID property of control in your content page

<script type="text/javascript">
var control = document.getElementById("<%=YourControl.ClientID%>")
</script> 

On master page, your code should be,

<script type="text/javascript">
function IsTextBoxExist(){
   if(control != null){
    ...
   }

}
</script>

Be sure that IsTextBoxExist calls when the page loading complete. like

<body onload="isTextBoxExist()"
Adeel
Nope, your solution doesn't work either, sorry....
Night Shade
+1  A: 

If you can use jQuery, you can use an "Attribute Contains Word" selector. Say your server-side text box id is 'text_box'. The client-side 'name' attribute will contain the server-side id (something like "ctl00$ctl00$text_box). So, to select it, use:

var textBox = $("input[name~=text_box]");
if (textBox.length != 0) {
  // text box found - do something here
}

Without jQuery, you could iterate through the form.elements collection and test each name:

for (var i; i++; i < forms[0].elements.length) {
  if (forms[0].elements[i].name.indexOf("text_box") != -1) {
    // text box found - do something here
    break;
  }
}
Ray
But he must be absolutely sure that the ID his is looking for is not substring of any other control in his page...
anthares
good point - it must be unique
Ray