System. Web. UI. webcontrols;
NamespaceDOTNET. Common. util
{
/// <Summary>
/// Control enumeration. When we disable or enable this enumeration, we use this enumeration to match the appropriate items.
/// </Summary>
Public Enum Controlnameenum
{
Panel = 0 , // Container
Textbox = 1 ,
Button= 2,//This is also commonly used, for example, disabling a button after it is submitted and enabling it after returning the result
Checkbox= 3,
Listcontrol= 4,
All= 100 //All
}
Public Static ClassControlhelper
{
# RegionDisable or enable some controls on the page at the same time.
/// <Summary>
/// Set whether to enable controls
/// </Summary>
/// <Param name = "control"> </param>
/// <Param name = "controlname"> </param>
/// <Param name = "isenable"> </param>
Public Static Void Setcontrolsenabled (Control, controlnameenum controlname, Bool Isenabled)
{
Foreach (Control item In Control. Controls)
{
/* We only consider several common Asp.net server controls and HTML controls. */
// Panel
If (Item Is Panel && (Controlname = Controlnameenum. Panel | Controlname = Controlnameenum. All ))
{
(Panel) item). Enabled = Isenabled;
}
// Textbox, htmltextbox
If (Controlname = Controlnameenum. textbox | Controlname = Controlnameenum. All)
{
If (Item Is Textbox)
{
(Textbox) (item). Enabled = Isenabled;
}
Else If (Item Is Htmlinputtext)
{
(Htmlinputtext) item). Disabled = Isenabled;
}
Else If (Item Is Htmltextarea)
{
(Htmltextarea) (item). Disabled = Isenabled;
}
}
// Buttons
If (Item Is Button && (Controlname = Controlnameenum. Button | Controlname = Controlnameenum. All ))
{
If (Item Is Button)
{
(Button) (item). Enabled = Isenabled;
}
Else If (Item Is Htmlinputbutton)
{
(Htmlinputbutton) (item). Disabled = ! Isenabled;
}
Else If (Item Is Imagebutton)
{
(Imagebutton) (item). Enabled = Isenabled;
}
Else If (Item Is Linkbutton)
{
(Linkbutton) (item). Enabled = Isenabled;
}
}
// Checkbox
If (Controlname = Controlnameenum. checkbox | Controlname = Controlnameenum. All)
{
If (Item Is Checkbox)
{
(Checkbox) (item). Enabled = Isenabled;
}
Else If (Item Is Htmlinputcheckbox)
{
(Htmlinputcheckbox) (item). Disabled = ! Isenabled;
}
}
// List controls
If (Controlname = Controlnameenum. listcontrol | Controlname = Controlnameenum. All)
{
If (Item Is Dropdownlist)
{
(Dropdownlist) (item). Enabled = Isenabled;
}
Else If (Item Is Radiobuttonlist)
{
(Radiobuttonlist) (item). Enabled = Isenabled;
}
Else If (Item Is Checkboxlist)
{
(Checkboxlist) (item). Enabled = Isenabled;
}
Else If (Item Is ListBox)
{
(ListBox) (item). Enabled = Isenabled;
}
Else If (Item Is Htmlselect)
{
(Htmlselect) (item). Disabled = ! Isenabled;
}
}
// If the project has child controls, call this function recursively.
If (Item. Controls. Count > 0 )
{
Setcontrolsenabled (item, controlname, isenabled );
}
}
}
# Endregion
}
}
The call in the ASPX page is as follows: Code
Protected Void Page_load ( Object Sender, eventargs E)
{
If ( ! Ispostback)
{
Controlhelper. setcontrolsenabled ( This . Page, controlnameenum. panel, False ); // Disable panel
}
}
It should be noted that the implementation here is only for several common controls, you can expand as needed by your own project.
Demo download: Demo