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)?
LVL 1
ITsolutionWizardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
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 MacDonaldDirector, Information SystemsCommented:
I think the OP wants to know how to remove all the controls' values, not remove the controls themselves.
kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
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

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

ITsolutionWizardAuthor Commented:
kaufmed: not working. all value is still there after the asp button is clicked.
kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
In which code-behind method (or more correctly, which event handler) are you attempting to use this code?
kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
To elaborate on my question, if you're trying to clear these values too soon in the page lifecycle, then ASP.NET will overwrite your values with that which is in the ViewState (provided you haven't disabled ViewState). ViewState is rehydrated between Init and Load. If you clear the values anywhere from Load/Page_Load onward, then it should work fine.

e.g.

Screenshot

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.