Asp.net Method for clearing control values (the control type can be customized)

Source: Internet
Author: User

Due to the end of the project, I have been busy optimizing some methods and made some suggestions for you.

When there are many controls in the page, it is too troublesome to clear their values one by one. So I wrote this method to customize the type of the clear control and flexibly respond to business needs.
Copy codeThe Code is as follows:
/// <Summary> Reset method control type enumeration </summary>
/// <Remarks> knowledge domain http://www.qqextra.com </remarks>
Public enum ReSetType
{
/// <Summary>
/// TextBox
/// </Summary>
TXT,
/// <Summary>
/// DropDownList
/// </Summary>
DDL,
/// <Summary>
/// RadioButtonList
/// </Summary>
RBL,
/// <Summary>
/// All ReSetType types
/// </Summary>
ALL
}
/// <Summary> Reset the control value </summary>
/// <Remarks> knowledge domain http://www.qqextra.com </remarks>
/// <Param name = "control"> this </param>
/// <Param name = "rst"> ReSetType. ALL indicates clearing 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 in 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
// List of radio buttons
If (c is RadioButtonList & blRbl = true)
{
(RadioButtonList) c). SelectedIndex =-1;
}
Else
If (c. HasControls ())
{
// Recursion
ReSet (c, rst );
}
}
}

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.