Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear asp.net form at runtime

Whats the easiest way to clear an asp.net form at runtime using c#.

Thanks Sp

like image 922
Steven Avatar asked Dec 04 '25 15:12

Steven


1 Answers

I assume you want to clear input boxes, dropdowns etc. This can be done the following way in code to recursivly clear all data.

foreach( var control in this.Controls )
{
   ClearControl( control );
}

and the recursive function

private void ClearControl( Control control )
{
    var textbox = control as TextBox;
    if (textbox != null)
        textbox.Text = string.Empty;

    var dropDownList = control as DropDownList;
    if (dropDownList != null)
        dropDownList.SelectedIndex = 0;

    // handle any other control //

    foreach( Control childControl in control.Controls )
    {
        ClearControl( childControl );
    }
}
like image 173
Mikael Svenson Avatar answered Dec 07 '25 04:12

Mikael Svenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!