Select All textbox on the page, and assign values to the two methods-from the server side and JS implementation

Source: Internet
Author: User


I saw a question today and asked:How can I select all textbox on the page and assign all values to string. Empty?The first thing I think of is to use Js for implementation. The common form is document. getelementbyid (), which is obviously not acceptable here. On the browser side, both the textbox of the server control and the <input type = "text"> of HTML are parsed in a unified manner. Therefore, it is not implemented at the end.

After searching on the internet, we found that to select and assign values to the text box, there are slave servers and JSCodeThere are two implementations. Based on the general situation of this question, we should use the server-side implementation method.


Server implementation:

Set a simple environment: an HTML text box and two server textbox boxes. After that, use the JS implementation button and the server-side implementation button. The design diagram is as follows:


The background code of the corresponding page is as follows (that is, the trigger event function of the button ):

Protected   Void Button#click ( Object Sender, eventargs E)
// Select all the controls in form1, which is simple. recursion is not implemented here, but a single-layer selection is implemented.
Foreach (Control CT In   This . Findcontrol ( " Form1 " ). Controls)
If (CT Is Textbox)
(Textbox) CT). Text =   String . Empty;


The result after the button event is triggered is as follows:

This allows you to select and assign values to textbox on the server.


JS Code implementation:

It is implemented using JS Code because it parses HTML code, so it cannot effectively distinguish the same code generated by the HTML text box and the textbox on the server side, but Selects all and assigns values at the same time. See JS Code:

< Script Type = " Text/JavaScript " Language = " Javascript "   >
// Description: describes how to select and assign values to all text boxes implemented by Js.

// Copyright:

// Notes: The simplest form of implementation

Function Setatjs ()
// Select all elements marked with input I also want to useGetelementsbytagnameBut I don't know how to get it ?!
VaR Arrtextbox = Document. getelementsbytagname ( " Input " );
For ( VaR I = 0 ; I < Arrtextbox. length; I ++ )
If (Arrtextbox [I]. Type = " Text " )
Arrtextbox [I]. Value = "" ;

The result is displayed when the button that triggers the JS function is started.



It can be seen that all the text boxes are empty!


Summary: through the simple examples above, we further studied the selection of server controls and elements in Js. In this regard, we must continue to strengthen it because it is too unfamiliar ..





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: 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.