views:

377

answers:

3

I have a master page which contains everything that inherits it within a form. A page inheriting from it needs to run some javascript to act on a text field on a page. However, I can't seem to reference that text field through the javascript, since the form begins on the master page.

The following line will come up bogus:

document.form1.txtFindUser.value = blah.responseText;

This is because form1 is defined on the master page, while txtFindUser is on the current page.

How can these situations be handled?

+4  A: 

How about that :

document.getElementById('txtFindUser').value = blah.responseText;

But if txtFindUser is your server control's id, then you should use the code above like that :

document.getElementById('<%= txtFindUser.ClientID %>').value = blah.responseText;
Canavar
i had tried getElementById but putting it in the <% %> tags ran past me. Thanks!
Chris
+1  A: 

Page elements don't always have the same name at runtime than they do in design time in .NET. View the source of your page when you ran it and see what txtFindUser is called after it was interpreted by IIS.

Mr. Smith
A: 

any server controls on the page will render with a different name since that page is derived from a master page. (I guess this removes complexities which can arise out of using same names for controls in your master page as well as in the derived page).

var ctrl = '<%= txtBox1.ClientID %>';
document.getElementById(ctrl).value='Hello world';

Refer to this post for more info

deostroll