Friday, 19 October 2012

clear all controls of a form with foreach loop in asp.net with c#


 

clear(Page);






private void clear(Control sender)
    {
        TextBox txt;
        DropDownList ddl;
        RadioButtonList rdb;
        foreach (Control cnt in sender.Controls)
        {
            if (cnt is TextBox)
            {
                txt = (TextBox)cnt;
                txt.Text = "";
            }
            else if (cnt is DropDownList)
            {
                ddl = (DropDownList)cnt;
                ddl.SelectedIndex = 0;
            }
            else if (cnt is RadioButtonList)
            {
                rdb = (RadioButtonList)cnt;
                rdb.SelectedIndex = 0;
            }
            if (cnt.HasControls())
            {
                clear(cnt);
            }
        }
    }

No comments:

Post a Comment