Method to reset members inside groupBox

前端 未结 3 1675
醉酒成梦
醉酒成梦 2020-12-22 05:35

is there any method for groupBox to clear all properties of objects inside groupBox. for example clear all textboxes, deselect all checkboxes etc. and set them to default. o

3条回答
  •  一个人的身影
    2020-12-22 06:34

    The fastest way to do that is :

    Control myForm = Page.FindControl("Form1");
    foreach (Control ctrl in myForm.Controls)
    {
        //Clears TextBox
        if (ctrl is System.Web.UI.WebControls.TextBox)
        {
            (ctrl as TextBox).Text = "";
        }
        //Clears DropDown Selection
        if (ctrl is System.Web.UI.WebControls.DropDownList)
        {
             (ctrl as DropDownList).ClearSelection();
        }
        //Clears ListBox Selection
        if (ctrl is System.Web.UI.WebControls.ListBox)
        {
            (ctrl as ListBox).ClearSelection();
        }
        //Clears CheckBox Selection
        if (ctrl is System.Web.UI.WebControls.CheckBox)
        {
            (ctrl as CheckBox).Checked = false;
        }
        //Clears RadioButton Selection
        if (ctrl is System.Web.UI.WebControls.RadioButtonList)
        {
            (ctrl as RadioButtonList).ClearSelection();
        }
        //Clears CheckBox Selection
        if (ctrl is System.Web.UI.WebControls.CheckBoxList)
        {
            (ctrl as CheckBoxList).ClearSelection();
        }
    }
    

提交回复
热议问题