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 :
Copy code The Code is as follows: 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;
Namespace DOTNET. 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 this is more common
Textbox = 1,
Button = 2, // This is also commonly used, such as disabling after button submission and enabling after return results
Checkbox = 3,
Listcontrol = 4,
All = 100 // All
}
Public static class controlhelper
{
# Region disable or enable some controls on the page at the same time
/// <Summary>
/// Set whether to enable the control
/// </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:Copy codeThe Code is as follows: protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Controlhelper. setcontrolsenabled (this. Page, controlnameenum. Panel, false); // disable a panel.
}
}
It should be noted that the implementation here is only for several common controls, you can expand as needed by your own project.
Test package download