Avatar of ITsolutionWizard
ITsolutionWizard
Flag for United States of America asked on

remove all of the value in the web form

in asp.net web form code behind. how to code a way to remove all of the asp.net control such as textbox, dropdown VALUE (in a for loop)?
ASP.NETC#

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
kaufmed

Similar to how I answered your other question, loop over the appropriate container's Controls collection, but do so using a for loop, and just call the RemoveAt method.

e.g.

for (int i = form.Controls.Count - 1; i >= 0; i--)
{
    form.Controls.RemoveAt(i);
}

Open in new window

Paul MacDonald

I think the OP wants to know how to remove all the controls' values, not remove the controls themselves.
kaufmed

If that be the case:

foreach (Control formControl in this.Form.Controls)
{
    TextBox txt = formControl as TextBox;

    if (txt != null)
    {
        txt.Text = string.Empty;
        continue;
    }

    DropDownList ddl = formControl as DropDownList;

    if (ddl != null)
    {
        ddl.SelectedIndex = -1;
        continue;
    }

    RadioButton radio = formControl as RadioButton;

    if (radio != null)
    {
        radio.Checked = false;
        continue;
    }

    CheckBox check = formControl as CheckBox;

    if (check != null)
    {
        check.Checked = false;
        continue;
    }
}

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ITsolutionWizard

ASKER
kaufmed: not working. all value is still there after the asp button is clicked.
kaufmed

In which code-behind method (or more correctly, which event handler) are you attempting to use this code?
ASKER CERTIFIED SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question