asp.net simple implementation Disable or enable a type of control on a page _ Practical tips

Source: Internet
Author: User
Tags static class
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.