Asp. NET pick-up-server-side control articles (i)

Source: Internet
Author: User
Tags gettext client

Tip: Get the value of a server-side control in a client's JavaScript script

Previously, when we needed to access an object within a page in a script, it was usually through the object's ID or name. Just like this--

// ...
function GetText ()
{
return document.form1.Text1.value; TEXT1 is the ID of the object.
}

// ...

Now, ASP. NET makes us more and more accustomed to using a textbox as a way of user input. If we wanted to access a textbox in client script, the original approach would be unworkable-

// ...
function GetText ()
{
return document.form1.Text1.value; TEXT1 or the ID of an object?
}

// ...

When browsing the page, there is a script error--"Text1 object does not exist." The reason is that Text1 as a server-side control textbox is converted by the. NET framework before being sent to the client, and its ID is clearly part of the transformation. If you look at the source of the page on the client side, you can see that the original TEXT1 no longer exists and is replaced by an ordinary input--

This is the result of the conversion, and the ID is no longer the ID specified at design time. If we want to access this text input box on the client, we must also change the ID of the access. How to change? directly to
Document.form1.Text1
To
Document.form1.item ("Test_text1")//safe, use item to get control by ID or name
Or
document.getElementById ("Test_text1")//safe, use getElementById to get control by ID or name

May I? Of course! As long as your control ID is fixed to "Text1".

However, only this condition is not enough. What's "Test" again? It should also be taken into account (fortunately, the ID of form will not change, or there will be more than one content to be concerned about).

As you may have seen, test is the name of this web page. Is that right? --Not exactly: P

Specifically, the "Test" in the ID after the conversion of the control is the ClientID of the Web Form object in which it resides. All of the ASP.net objects have an instance on the server (if you don't have enough of your object-oriented base to do it again), and this "Test" is the ClientID of this page instance object. ClientID, then, is a property of each Web Forms page that indicates the identity of the Web form on the client.

Why are you so complicated? The simple truth is that we can't determine the ClientID of the page and the ID of the control in client script.
How should we do that?

"Generates client JavaScript in server-side code. "--it seems very complicated, but it's not difficult, as long as the server-side Page_Load event is added (outside the IsPostBack judgment)-
RegisterStartupScript ("Start",
"\ n");

RegisterStartupScript is a method of Web forms (System.Web.UI.Page classes) that registers client script in a generated page.

Here, we add a gettext () function that works just like the previous gettext (), except that the control ID it accesses is not specified in the script. Instead, it is generated dynamically on the server side based on the ClientID of the page (This.clientid,this is the page itself) and the Text1 control's ID (This.Text1.ID).

After compiling and browsing again, we will find this JavaScript function generated by server-side code in the new page source code. At this point, calling the GetText () function elsewhere in the page will get the contents of the Text1 right.




Related Article

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.