Error in javascript codings while loading content page

V

Valli

HI,

I have a master page & a content page in my project. My content page
contains javascript codings.
While I load my content page in browser(IE) , I ma getting an error (Error
is displayed in the status bar). After i click the status bar, the error
dispayed was "object required ".

My javascript code follows...

function window.onload()
{
document.getElementById('Update').style.visibility='hidden'; --Error
occurs here (object required)
}

Is there any wrong in using "document.getElementById" to access a control
in the form? or do I need to use some other property to access a control in
the form in javascript.

Can anyone help me
 
N

Nick Chan

Are you sure there's element with ID "Update" in the form?

and the syntax is window.onload=function()
{ document.getElementById('Update').style.visibility = 'hidden' }
 
V

Valli

My content page works fine when it doesnt have a masterpage.
If I include my mster page, it provideks error.

My script coding is under <asp:content> tag.
 
V

Valli

Yes. There is an element with ID 'Update'.
It works fine if master page is not included....

I view the source of the browser page & the error was pointed in this
javascript line.
If I comment this line & load the page , the next line throws error where
that line also access another control in the form.

--
Regards
Valli


Nick Chan said:
Are you sure there's element with ID "Update" in the form?

and the syntax is window.onload=function()
{ document.getElementById('Update').style.visibility = 'hidden' }
 
B

Brian K. Williams

If you are using a master page and have a control with an ID of "Update"
inside the Content tag, then it can't exist as simply "Update" because it
will be prefixed with additional control IDs. The control will be prefixed
with a control number followed by the Content Place Holder ID, any nested
controls, then the control id.



For example, if I had a Default Page as:

<asp:Content ID="DefaultContent"
ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server">

<asp:Table ID="tblPageActions".

</asp:Content>



The ID when viewing the source would be,
ctl00_ContentPlaceHolderMain_tblPageActions



If I was to move this <asp:Table ID="tblPageActions". into a User Control
with an ID of MyUserControl then the ID would become:

ctl00_ContentPlaceHolderMain_MyUserControl_tblPageActions.



If I was to add this control again to the page the second control would be
an ID:

ctl01_ContentPlaceHolderMain_MyUserControl_tblPageActions.





The safest was to get the actual ID is to use the Control.ClientID:

document.getElementById("<%=tblPageActions.ClientID%>");





Add this script to your master page to view the IDs.

<script language="javascript" type="text/javascript">
document.onmouseover = ShowId;
function ShowId()
{
window.status = event.srcElement.id;
}
</script>



You can also download the Internet Explorer Developer Toolbar to view the
IDs and much more.

https://www.microsoft.com/downloads...64-672D-4511-BB3E-2D5E1DB91038&displaylang=en







Regards,

Brian K. Williams
 
B

bruce barker

because Update is contained in a container control (master), its
ClientID also contains the container name, so you getElementById return
null. try:


document.getElementById('<%=Update.ClientID%>').style.visibility='hidden';



-- bruce (sqlwork.com)
 
N

Nikron

Valli said:
HI,

I have a master page & a content page in my project. My content page
contains javascript codings.
While I load my content page in browser(IE) , I ma getting an error (Error
is displayed in the status bar). After i click the status bar, the error
dispayed was "object required ".

My javascript code follows...

function window.onload()
{
document.getElementById('Update').style.visibility='hidden'; --Error
occurs here (object required)
}

Is there any wrong in using "document.getElementById" to access a control
in the form? or do I need to use some other property to access a control in
the form in javascript.

Can anyone help me
Hi,

When the master page renders out your content page it adds its' own
prefix to all the controls on your content form, something like
'ctl00_m_cphMain_' and whatever the name of your control is. So if your
control is named 'Update' then the master page will add the prefix
'ctl00_MasterPageName_' to the front of it to produce
'ctl00_MasterPageName_Update'.

To find out what your control is called when it's rendered, open your
page in the browser and do a find for 'Update' then you'll see the
master pages' prefix there and you use that in your javascript call.

Hope this helps

Nikron
 
V

Valli

Hi all,

Thanks a lot.

I got the clientId of the control & used it in javascript. It works
perfectly.

I have updated my code as ...
document.getElementById('<%=Update.ClientID%>').style.visibility='hidden';
instead of
document.getElementById('Update').style.visibility='hidden';

Regards
Valli










Brian K. Williams said:
If you are using a master page and have a control with an ID of "Update"
inside the Content tag, then it can't exist as simply "Update" because it
will be prefixed with additional control IDs. The control will be prefixed
with a control number followed by the Content Place Holder ID, any nested
controls, then the control id.



For example, if I had a Default Page as:

<asp:Content ID="DefaultContent"
ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server">

<asp:Table ID="tblPageActions".

</asp:Content>



The ID when viewing the source would be,
ctl00_ContentPlaceHolderMain_tblPageActions



If I was to move this <asp:Table ID="tblPageActions". into a User Control
with an ID of MyUserControl then the ID would become:

ctl00_ContentPlaceHolderMain_MyUserControl_tblPageActions.



If I was to add this control again to the page the second control would be
an ID:

ctl01_ContentPlaceHolderMain_MyUserControl_tblPageActions.





The safest was to get the actual ID is to use the Control.ClientID:

document.getElementById("<%=tblPageActions.ClientID%>");





Add this script to your master page to view the IDs.

<script language="javascript" type="text/javascript">
document.onmouseover = ShowId;
function ShowId()
{
window.status = event.srcElement.id;
}
</script>



You can also download the Internet Explorer Developer Toolbar to view the
IDs and much more.

https://www.microsoft.com/downloads...64-672D-4511-BB3E-2D5E1DB91038&displaylang=en







Regards,

Brian K. Williams
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top