For example, when submitting a form, it may be slow to process because of a network or server, and the user repeatedly clicks on the button to submit before the result is processed. This can easily cause unnecessary trouble and even error. To say so much is to implement a feature that disables certain controls. OK, I'll introduce myself to this small function of simple implementation, paste code:
Copy Code code 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, which, when disabled or enabled, matches the appropriate item based on this enumeration
</summary>
public enum Controlnameenum
{
Panel = 0,//container This comparison is commonly used
TextBox = 1,
button = 2,//This is also more commonly used, for example, after the button is submitted to disable, after the result is enabled
CheckBox = 3,
ListControl = 4,
all = 100/All
}
public static Class Controlhelper
{
#region Disable or enable some controls on a page at the same time
<summary>
Setting whether controls are enabled
</summary>
<param name= "Control" ></param>
<param name= "ControlName" ></param>
<param name= "Isenable" ></param>
public static void setcontrolsenabled (Control control, Controlnameenum controlname, bool isenabled)
{
foreach Control item. 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 also has child controls, the function is called recursively
if (item. Controls.Count > 0)
{
Setcontrolsenabled (item, controlname, isenabled);
}
}
}
#endregion
}
}
The calls in the ASPX page are as follows:
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Controlhelper.setcontrolsenabled (this. Page, Controlnameenum.panel, false); Panel disabled
}
}
Note that my implementation here is only for a few common controls, and you can expand as you want for your project.
Test Package Download