Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag 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)?
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

I think the OP wants to know how to remove all the controls' values, not remove the controls themselves.
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

Avatar of ITsolutionWizard

ASKER

kaufmed: not working. all value is still there after the asp button is clicked.
In which code-behind method (or more correctly, which event handler) are you attempting to use this code?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial