The client obtains the Server Control ID.

Source: Internet
Author: User

In the Javascript script of the client, you must first obtain the reference of the control when you want to operate the server control. Generally, the ID of the server control on the Web page is obtained first. by calling the method of the documnet object, you can obtain the reference of the server control.

For example, a server control is defined on a webpage.

<Asp: dropdownlist id = "equipmentlist" runat = "server" width = "96px"> </ASP: dropdownlist>

In the script, you can use the VaR list = document. getelementbyid ("equipmentlist"); obtain the reference of the control, and then you can operate the control, such as list. options. length = 3; you can also use document. all ["equipmentlist"]. options. length = 3; an implementation. I have just learned how to use web development.

Problem: a user control is defined. The user control contains several server controls, and a script is written into the user control to control the server control. Still follow the above method, error. The prompt is: the document. getelementbyid ("equipmentlist") is null or not an object, indicating that the equitmentlist control is not found and no reference is obtained. After instruction, it is learned that when a user control is placed on a webpage, the ID of its internal server control will change, not the ID defined in the user control. To obtain this ID, use the following statement: <% = Server Control name. clientid %> to modify the script in the user control.Code, Replace the original ID with: <% = Server Control name. clientid %>, for example:

VaR list = Document. getelementbyid ("equipmentlist"); changed to VaR list = Document. getelementbyid ("<% = equipmentlist. clientid %>"); the Code runs successfully.

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.