Preface:
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: http://www.cnblogs.com/yangmingming
// 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 ..