Method to reset members inside groupBox

孤人 提交于 2019-11-29 18:12:20
Kaveh Kamali

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();
    }
}

You would need to clear all the controls inside the group box one by one.

You would have to make a function like this:

private void ClearControls(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;

    // And add any other controls
    // ...

    foreach( Control childControl in control.Controls )
    {
        ClearControl( childControl );
    }
}

Just call it like this:

ClearControls(this);

This will work recursively, so if you have any panels, for example, with their own set of controls to clear, this will clear those too.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!