针对复选框启用禁用下拉列表

enable disable dropdownlist with respect to checkbox

我在 RadGrid 中有一个 dropdownlist 和一个 checkbox

我最初设置了下拉菜单 Enabled="false"。所以在页面加载时,下拉列表将被禁用,现在如果我选中 RadGrid 中的任何复选框,下拉列表应该被启用。

复选框:

<telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
    <ItemTemplate>
        <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="ToggleRowSelection"
            AutoPostBack="True" />
    </ItemTemplate>
    <HeaderTemplate>
        <asp:CheckBox ID="headerChkbox" runat="server" OnCheckedChanged="ToggleSelectedState"
            AutoPostBack="True" />
    </HeaderTemplate>
</telerik:GridTemplateColumn>

下拉列表:

<telerik:RadDropDownList ID="ddlAction" Enabled="false" DefaultMessage="Action" AutoPostBack="true" DataValueField="action" OnSelectedIndexChanged="ddlAction_SelectedIndexChanged"  runat="server">
   <Items>
       <telerik:DropDownListItem Text="" Value="" />
       <telerik:DropDownListItem Text="Refresh Mail Status" Value="Refresh Mail Status" />
       <telerik:DropDownListItem Text="Send All" Value="Send All" />
       <telerik:DropDownListItem Text="Send Selected" Value="Send Selected"/>
       <telerik:DropDownListItem Text="Remove" Value="Remove" />
   </Items>
</telerik:RadDropDownList>

后面的代码:

protected void ddlAction_SelectedIndexChanged(object sender, DropDownListEventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    chk.FindControl("CheckBox1");

    if (!chk.Checked) {
        ddlAction.Enabled = true;
    }
}

切换行选择

protected void ToggleRowSelection(object sender, EventArgs e)
        {
            ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
            bool checkHeader = true;
            foreach (GridDataItem dataItem in radgridCCBList.MasterTableView.Items)
            {
                if (!(dataItem.FindControl("CheckBox1") as CheckBox).Checked)
                {
                    checkHeader = false;
                    break;
                }
            }
            GridHeaderItem headerItem = radgridCCBList.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
            (headerItem.FindControl("headerChkbox") as CheckBox).Checked = checkHeader;

        }

ToggleSelectedState

protected void ToggleSelectedState(object sender, EventArgs e)
        {
            CheckBox headerCheckBox = (sender as CheckBox);
            foreach (GridDataItem dataItem in radgridCCBList.MasterTableView.Items)
            {
                (dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
                dataItem.Selected = headerCheckBox.Checked;
            }
        }

这里,ToggleRowSelectionToggleSelectedState 方法是 select 当我选中特定行的复选框时,select 特定的网格行。

请尝试使用以下代码片段。

方法一:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlAction.Enabled = false;
    }
}

protected void ToggleRowSelection(object sender, EventArgs e)
{
    ValidateCheckbox();
}

protected void ValidateCheckbox()
{
    bool IsEnabled = false;

    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        CheckBox CheckBox1 = item["CheckBoxTemplateColumn"].FindControl("CheckBox1") as CheckBox;
        if (CheckBox1.Checked)
        {
            IsEnabled = true;
            break;
        }
    }

    ddlAction.Enabled = IsEnabled;
}

方法二:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlAction.Enabled = false;
    }
}


protected void Page_PreRender(object sender, EventArgs e)
{
    ValidateCheckbox(); 
}

protected void ValidateCheckbox()
{
    bool IsEnabled = false;

    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        CheckBox CheckBox1 = item["CheckBoxTemplateColumn"].FindControl("CheckBox1") as CheckBox;
        if (CheckBox1.Checked)
        {
            IsEnabled = true;
            break;
        }
    }

    ddlAction.Enabled = IsEnabled;
}

更新 1:

protected void ValidateCheckbox()
{
    bool IsEnabled = false;

    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        CheckBox CheckBox1 = item["CheckBoxTemplateColumn"].FindControl("CheckBox1") as CheckBox;
        if (CheckBox1.Checked)
        {
            IsEnabled = true;
            break;
        }
    }

    GridHeaderItem headerItem = radgridCCBList.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
    if ((headerItem.FindControl("headerChkbox") as CheckBox).Checked)
    {
        IsEnabled = true;
    }

    ddlAction.Enabled = IsEnabled;
}

更新 2:(仅禁用 RadDropDownList 中的项目)

ASPX

<telerik:RadDropDownList ID="ddlAction" DefaultMessage="Action" AutoPostBack="true" DataValueField="action" runat="server">
    <Items>
        <telerik:DropDownListItem Text="" Value="" />
        <telerik:DropDownListItem Text="Refresh Mail Status" Value="Refresh Mail Status" />
        <telerik:DropDownListItem Text="Send All" Value="Send All" />
        <telerik:DropDownListItem Text="Send Selected" Value="Send Selected" />
        <telerik:DropDownListItem Text="Remove" Value="Remove" />
    </Items>
</telerik:RadDropDownList>

ASPX.CS

protected void Page_Load(object sender, EventArgs e)
{
    // To disable "Send All" option in DropDown
    DisableItemInDropDown("Send All");

    // To disable "Remove" option in DropDown
    DisableItemInDropDown("Remove");
}

protected void DisableItemInDropDown(string ddlItemValueText)
{
    foreach (DropDownListItem item in ddlAction.Items)
    {
        if (item.Value == ddlItemValueText)
        {
            item.Enabled = false;
        }
    }
}