Reflection traversal textbox

Source: Internet
Author: User

It's quite difficult at noon, so listen to songs and record the morning stuff.

On the page where the file was uploaded yesterday, you need to record some information while uploading the file. After uploading the file, you need to clear the entered content. (Textbox is used)

In fact, this can be processed using JavaScript. In addition, on the client side, the pressure on the server is reduced. As an exercise, I chose to process it on the server.

Traverse all the controls on the page and judge whether it is actually not a textbox. If yes, the value is null.

Private void clear ()
{
Foreach (Control in this. findcontrol ("form1"). Controls)
{
If (control is textbox)
{
(Textbox) control). Text = string. empty;
// Clear (control. Controls );
}
}
}

 

This idea is the easiest to think of. Of course, when I look at similar problems on the internet, I find that reflection can also be used to solve the problem. Although for a simple traversal, the efficiency of using reflection may not be high, but for the purpose of learning, try learning ~ I was just reading a series of reflection articles from Ziyang recently.

 

Private void clear2 ()
{
Type T = typeof (textbox );
Fieldinfo [] Infos = This. GetType (). getfields (bindingflags. getfield | bindingflags. Public | bindingflags. nonpublic | bindingflags. instance );
For (INT I = 0; I <Infos. length; I ++)
{
If (Infos [I]. fieldtype. Name = T. Name)
{
(Textbox) Infos [I]. getvalue (this). Text = string. empty;
}
}
}

First, obtain the textbox type, then obtain the type in the current instance, use the binding constraint, search for the field currently defined by system. type, and store it in the Infos array.

Then, the system goes to the traversal array and determines whether it is a textbox type. If yes, the value is changed. This is just a matter of ignorance and remains to be studied.

The function is implemented, not completely mastered

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.