0

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?

3 Answers 3

4

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;
1
  • i had tried getElementById but putting it in the <% %> tags ran past me. Thanks!
    – Chris
    Aug 7, 2009 at 14:35
1

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.