Use Javascript in masterpage to get objects

Source: Internet
Author: User

Masterpage is a new thing in vs.net 2005. It seems that many people have not used it yet. The specific usage of masterpage is explained in more detail in the tutorials.
I encountered a problem when using it, that is, when using JavaScript in the masterpage content page to obtain the Server Control in the content page, the object cannot be obtained.

At that time, my code was like this. For example:

The following sections describe how to use the javasrcipcommand:

Function A () {var txt1 = Document. getelementbyid ("txt1 ");}

The page has only one Textbox, id = txt1;
The above code may cause errors. The debugging information is that JavaScript cannot obtain the object. After debugging, I found that after the page is generated, I checked the source code and found that the textbox ID on the content page will be changed to something like ctl00 $ contentplaceholder1 $ txt1

Therefore, one of the solutions is derived here. I tried to replace txt1 in JavaScript with ctl00 $ contentplaceholder1 $ txt1. The program can work.

However, this will cause a problem. Ctl00 $ contentplaceholder1 $ txt1 is automatically generated by the program. We don't know when the program will change it. There is a lot of reliability in it. If the program changes this ID, the javascript I wrote will not work.

I have been asking this question for a long time on the Javascript version of csdn, and no one can answer it to me.

Fortunately, I found another relatively reliable solution and shared it.

The javascript part is changed to this

Function A () {var txt1 = Document. getelementbyid ("<% = txtvirusname. clientid %> ");}

In this way, you can use it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.