2017年8月9日 星期三

取得物件類別-typeof(DropDownList)/GetType()

private void ClearControlValue(params Control[] ctols)
{
    foreach (Control ctol in ctols)
    {
        string cType = ctols.GetType().Name;
        if (typeof(TextBox).Name == cType)
        {
            (ctol as TextBox).Text = string.Empty;
        }
        else if (typeof(DropDownList).Name == cType)
        {
            (ctol as DropDownList).Items.Clear();
        }
        else if (typeof(CheckBox).Name == cType)
        {
            (ctol as CheckBox).Checked = false;
        }
        else if (typeof(CheckBoxList).Name == cType)
        {
            (ctol as CheckBoxList).SelectedIndex = -1;
        }
        else if (typeof(RadioButton).Name == cType)
        {
            (ctol as RadioButton).Checked = false;
        }
        else if (typeof(RadioButtonList).Name == cType)
        {
            (ctol as RadioButtonList).SelectedIndex = -1;
        }
        else if (typeof(Label).Name == cType)
        {
            (ctol as Label).Text = string.Empty;
        }
    }
}

protected void DisableControls(Control parent, bool State) {
    foreach(Control c in parent.Controls) {
        if (c is DropDownList) {
            ((DropDownList)(c)).Enabled = State;
        }

        DisableControls(c, State);
    }
}

沒有留言:

張貼留言