Retrieve the name and value of the ASP. NET page control through Traversal

Source: Internet
Author: User

When I visited csdn, I encountered someone asking for help. I would like to share my experience with you.
If you directly use page. the control obtains only the top-level page elements, and the controls such as text boxes or labels that are really dragged and put are also hidden in these top-level page elements, so we need to traverse them again.
The function and usage method are as follows. The hashtable method is used to save the result.

Protected   Void Page_load ( Object Sender, eventargs E)
{
Getallcontrolvalue (This);
}

Hashtable getallcontrolvalue ( Object Pageorusercontrol)
{
Hashtable RTN =   New Hashtable ();

Foreach (Control CTR In (Pageorusercontrol As Page). Controls)
{
Getcontrolvalue (CTR, RTN );
}

Return RTN;
}

Void Getcontrolvalue (control ctrin, hashtable HT)
{
Foreach (Control CTR In Ctrin. Controls)
{
Type controltype = Ctr. GetType ();

Switch (Controltype. tostring ())
{
Case   " System. Web. UI. webcontrols. textbox " :
Textbox controltextboxobj = (Textbox) CTR;
String Controltextboxname = Controltextboxobj. ID;
String Controltextboxvalue = Controltextboxobj. text;
Ht. Add (controltextboxname, controltextboxvalue );
Break ;
Case   " System. Web. UI. webcontrols. Label " :
Label controllabelobj = (Label) CTR;
String Controllabelname = Controllabelobj. ID;
String Controllabelvalue = Controllabelobj. text;
Ht. Add (controllabelname, controllabelvalue );
Break ;
// Case "other types ":
// Other types: controltextboxobj = (other types) CTR;
// String controltextboxname = controltextboxobj. ID;
// String controltextboxvalue = controltextboxobj. text;
// Ht. Add (controltextboxname, controltextboxvalue );
// Break;
Default :
Break ;
}
}
}

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.