remove all of the value in the web form

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
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)?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
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 Systems

Commented:
I think the OP wants to know how to remove all the controls' values, not remove the controls themselves.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
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

Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
kaufmed: not working. all value is still there after the asp button is clicked.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
In which code-behind method (or more correctly, which event handler) are you attempting to use this code?
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial