Pages

Tuesday 8 January 2013

Clear Controls using Common Function

public static void ClearControls(ContentPlaceHolder cpl)
    {
        foreach (Control c in cpl.Controls)
        {
            if (c is TextBox)
            {
                ((TextBox)c).Text = string.Empty;
            }
            else if (c is DropDownList)
            {
                ((DropDownList)c).SelectedIndex = -1;
            }
            else if (c is RadioButtonList)
            {
                for (int i = 0; i <= ((RadioButtonList)c).Items.Count - 1; i++)
                {
                    ((RadioButtonList)c).Items[i].Selected = false;
                }
            }
            else if (c is CheckBoxList)
            {
                for (int i = 0; i <= ((CheckBoxList)c).Items.Count - 1; i++)
                {
                    ((CheckBoxList)c).Items[i].Selected = false;
                }
            }
        }
    }

No comments:

Post a Comment