0

I have one dropdownlist and a checkbox in RadGrid.

I have set dropdown Enabled="false" initially. So on page load, dropdownlist will be disabled, now if I check any checkbox from RadGrid, dropdown should be enabled.

Checkbox:

<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>

Dropdownlist:

<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>

Code behind:

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

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

ToggleRowSelection

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

Here, ToggleRowSelection and ToggleSelectedState method are to, select particular row of grid when i check a checkbox of that particular row.

6
  • Please provide code where you have added RadDropDownList. (it would be nice if you will provide your all columns markup/aspx code). Commented Jul 21, 2015 at 9:36
  • i have already posted.. RadDropDownList is above RadGrid Commented Jul 21, 2015 at 9:41
  • i am referring your tutorials for telerik. hope you can solve it. ;) @JayeshGoyani Commented Jul 21, 2015 at 9:43
  • Try to use EditItemTemplate instead of using ItemTemplate Commented Jul 21, 2015 at 9:58
  • I think your code says the checkbox methods are "ToggleRowSelection" and "ToggleSelectedState", but you didn't show what those methods do. Commented Jul 21, 2015 at 10:17

1 Answer 1

2

Please try with the below code snippet.

Method1:

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

Method2:

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

Update 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;
}

Update 2: (To disable only items in 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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

just for knowledge. is it possible that if checkbox is not checked, still dropdown will be enabled, but items in dropdownlist will be disabled? any idea?
I will try and inform you. (In office I don't have access of Telerik DLL)
logic of selecting row with respect to checkbox is breaking due to your code.
and if i uncheck checkbox, dropdown is still enabled.
Please try with Method 2 and let me know if it is not working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.