Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use foreach loop for clear the all textboxes?

Tags:

asp.net

c#-4.0

 public void clear()
  {
    lblage.Text = "";
    lblclosingbirds.Text = "";
    lbltypeoffeed.Text = "";
    lbltypeoffeedf.Text = "";
    lblstdfeed.Text = "";
    lblstdfeedf.Text = "";
    lblstdhd.Text = "";
    lblstdhe.Text = "";
    lblExpeggs.Text = "";
    lblbirdsf.Text = "";
    txtacteggs.Text = "";
    txtactfeed.Text = "";
    txtactfeedf.Text = "";      
    txtfemaleclosingstock50.Text = "";
    txtfemaleclosingstock70.Text = "";
    txtmale50kgcstock.Text = "";
    txtmale70kgscstock.Text = "";
    txtmort.Text = "";
    txtmortf.Text = "";
    txtuseeggs.Text = "";
    ddlFemaleFeedtype.SelectedValue = "0";
    ddlMaleFeedtype.SelectedValue = "0";

}

how to use foreach loop method for replace with clear()..please tell me.. any one... is it possible to write foreach loop...please tell me

like image 426
Sambasiva Avatar asked Jan 12 '23 05:01

Sambasiva


1 Answers

void ClearAllControlsRecursive(Control container)
{
    foreach(var control As Control in container.Controls)
    {
        if(typeof control is TextBox)
        {
            ((TextBox)control).Text = String.Empty;
        }
        if(control.HasControls())
        {
            ClearAllControlsRecursive(control);  
        }
    }
}

then call this method like this:

ClearAllControlsRecursive(yourContainer);

You can also switch on the control type and clear the values accordingly.

like image 118
Superzadeh Avatar answered Feb 23 '23 13:02

Superzadeh