How can I dynamically clear all controls in a user control?

Is it possible to dynamically (and in general) clear the state of all child user controls? (e.g. all of its text fields, DropDrownLists, RadioButtons, DataGrids, Repeaters, etc. - basically everything that ViewState has)

I try not to do something like this:

foreach (Control c in myUserControl.Controls)
{
    if (c is TextBox)
    {
        TextBox tb = (TextBox)c;
        tb.Text = "";
    }
    else if (c is DropDownList)
    {
        DropDownList ddl = (DropDownList)c;
        ddl.SelectedIndex = -1;
    }
    else if (c is DataGrid)
    {
        DataGrid dg = (DataGrid)c;
        dg.Controls.Clear();
    }

    // etc.

}

I am looking for something like this:

foreach (Control c in myUserControl.Controls)
    c.Clear();

... but obviously this does not exist. Is there an easy way to accomplish this dynamically / overall?

+5
source share
7 answers

, , ( ), essentailly (.. , ).

public static class ControlExtensions
{
    public static void Clear( this Control c )
    {
        if(c == null) {
            throw new ArgumentNullException("c");
        }
        if (c is TextBox)
        {
            TextBox tb = (TextBox)c;
            tb.Text = "";
        }
        else if (c is DropDownList)
        {
            DropDownList ddl = (DropDownList)c;
            ddl.SelectedIndex = -1;
        }
        else if (c is DataGrid)
        {
            DataGrid dg = (DataGrid)c;
            dg.Controls.Clear();
        }
        // etc....
    }
}

, /

foreach (Control c in myUserControl.Controls) {
    c.Clear();
}

, , control.Clear() - .

+3

foreach (Control c in myUserControl.Controls) {
    myUserControl.Controls.Remove(c);
}

Controls - , Remove() , , .

EDIT: , , . , , , -, Reflection, , , ,

foreach (Control c in myUserControl.Controls) {
    c = new c.Type.GetConstructor().Invoke();
}

- , .

+1

, viewstate usercontrol . :

UserControl:

public void Clear()
{
    this.ViewState.Clear();
}

:

myUserControlInstance.Clear();

. , StateBag UserControl, / . , , , viewstate :

UserControl:

public void Clear()
{
    ClearViewState(this.Controls);
}

private void ClearViewState(ControlCollection cc)
{
    foreach(Control c in cc)
    {
        if(c.HasControls())
        {
            //clear the child controls first
            ClearViewState(c.Controls);
        }        

        //then clear the control itself
        c.ViewState.Clear();
    }
}

:

myUserControlInstance.Clear();

. , , . Clear / , .

, !

+1
myUserControl.Controls.ToList().ForEach(c => myUserControl.Controls.Remove(c));

, . .

+1

EnableViewState="false" , .

+1

Control.ClearChildViewState?

MSDN

.

I have never used this. So I'm not sure if this will help you. Sounds good, I think :)

+1
source

Why not do as you suggest:

foreach (Control c in myUserControl.Controls)
    c.Clear();

And then we implement Clear:

public static class UserController
{
    public static void Clear( this Control c )
    {
        c.Controls.Clear();
    }

    public static void Clear( this TextBox c )
    {
        c.Text = String.Empty;
    }
}

That should do it.

0
source

All Articles