A function recently encountered in a winform project. After selecting a checkbox, other controls in the form are unavailable. In this case, this function is sometimes used in Asp.net projects. For example, when submitting a form, the processing may be slow due to network or server reasons, and the user clicks the button repeatedly before the processing result is displayed. This can easily cause unnecessary troubles or even errors. After talking about this, we actually need to implement a function to disable some controls. Now, I will introduce this small feature that I have simply implemented. Code : Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using 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