asp.net method to empty the value of a control (customizable control type) _ Practical Tips

Source: Internet
Author: User
Due to the closure of the project, I have recently been busy doing some optimization of methods to organize some share for everyone.

When there are many controls in the page, we need to clear the value of the time, it is too troublesome to empty. So you write a method that lets you customize the type of empty controls and respond flexibly to your business needs.
Copy Code code as follows:

<summary> Reset method control type enumeration </summary>
<remarks> knowledge Domain http://www.qqextra.com 2012-12-28</remarks>
public enum Resettype
{
<summary>
Textbox
</summary>
Txt
<summary>
DropDownList
</summary>
Ddl
<summary>
RadioButtonList
</summary>
Rbl
<summary>
All Resettype types
</summary>
All
}
<summary> Reset control's value </summary>
<remarks> knowledge Domain http://www.qqextra.com 2012-12-28</remarks>
<param name= "Control" >this</param>
<param name= "rst" >resettype.all to empty all control types contained in the Resettype enumeration </param>
public static void ReSet (control control, params resettype[] rst)
{
BOOL Bltxt = false;
BOOL Blddl = false;
BOOL BLRBL = false;
foreach (Resettype type in rst)
{
if (type = = Resettype.all)
{
Bltxt = true;
Blddl = true;
BLRBL = true;
Break
}
Else
if (type = = ReSetType.TXT)
Bltxt = true;
else if (type = = RESETTYPE.DDL)
Blddl = true;
else if (type = = RESETTYPE.RBL)
BLRBL = true;
}
foreach (Control C-control. Controls)
{
text box
if (c is TextBox && bltxt = = True)
{
((TextBox) c). Text = "";
}
Else
Drop down list
if (c is DropDownList && blddl = = True)
{
DropDownList DDL = (DropDownList) C;
if (DDL. Items.Count > 0)
{
Ddl. SelectedIndex = 0;
}
}
Else
radio button List
if (c is radiobuttonlist && BLRBL = = True)
{
((RadioButtonList) c). SelectedIndex =-1;
}
Else
if (C.hascontrols ())
{
Recursion
ReSet (c, RST);
}
}
}

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.