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 ;
}
}
}