ASP.NET Pickup - Server-side Controls (1) 〖This article has error awareness on server controls

zhaozj2021-02-11  222

Tip 1: Get the value of the server-side control in the client's JavaScript script, when we need to access an object within the script, generally pass the object's ID or NAME. like this--

// ... function getText () {returnction.form1.text1.value; // text1 is the ID} // ...

Now, ASP.NET makes us more and more common ways to use TextBox as a way to enter. If we want to access a TextBox in the client script, the original approach does not work -

// ...

Function GetText ()

{

Return Document.form1.text1.value; //

TEXT1 or the ID?

}

// ...

When browsing the page, there will be a script error - "Text1 object does not exist". The reason is that Text1 is used as a server-side control TextBox, and the .NET Framework is converted first before being sent to the client, and its ID is obviously part of the conversion. If you look at the source code of the page on the client, you can find that the original text1 does not exist, replaced by an ordinary input -

This is the result of the conversion, the ID is no longer the ID specified when design. If we want to access this text input box on the client, you must also change the ID of the access. How to change? Direct

Document.form1.text1

Change to

Document.form1.item ("Test_Text1") // Insurance Same, use Item by ID or Name to get controls

or

Document.getlementByid ("Test_Text1") // Insurance Same, use getElementByid by ID or Name to get controls

May I? of course can! As long as your control ID is fixed, it is "text1".

However, only this condition is not enough. What is "TEST"? It should also be considered (fortunately, the ID of the Form will not change, otherwise the content should be careful.).

You may have seen that TEST is the name of this web page. right? - Not completely: P

Specifically, "TEST" in the ID of the control conversion is the clientID of its web form object. All ASP.NET objects have an instance on the server side (if you are not the object's foundation, it is recommended to finish it), and this "Test" is the clientID of this page instance object. The clientID is an attribute of each web form page that indicates the identity of this web form on the client.

Why is it so complicated? The reason is very simple, we can't determine the client's clientID and control ID in the client script.

What should I do?

"Generate client JavaScript in server-side code" - seems very complicated, it is not difficult, just add (outside the ISPostBack judgment) in the server side (outside the ISPostBack judgment)

RegisterStartupScript ("start",

"/ n