This is a migrated thread and some comments may be shown as answers.

disable checkboxlist option

6 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Troika
Top achievements
Rank 1
Troika asked on 20 Sep 2013, 03:03 PM
how could i programatically disable a checkboxlist option wich is in radgrid edit forms mode

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 21 Sep 2013, 04:32 AM
Hi Troika,

Please try the following code snippet to disable a checkbox list in Edit Mode.

ASPX:
<EditItemTemplate>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" >
    </asp:CheckBoxList>
</EditItemTemplate>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {     
       if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
       {
           GridEditFormItem edititem = (GridEditFormItem)e.Item;
           CheckBoxList ckeck = (CheckBoxList)edititem .FindControl("CheckBoxList1");
           ckeck .Enabled = false;
       }
   }

Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 21 Sep 2013, 09:01 AM
hi,


thanks for your help, but I want to disable just one option in the checkboxlist.
the checkboxlist hs 3 items I want to disable one of them based on its name
0
Troika
Top achievements
Rank 1
answered on 21 Sep 2013, 09:34 AM
I have tried this:

~but cant find that checkboxlist :S

CheckBoxList checkedItems = EditItem["CBLRole"].Controls[0] as CheckBoxList;
 
                checkedItems.Items.FindByText("Administradores");
 
                checkedItems.Enabled = false;
0
Princy
Top achievements
Rank 1
answered on 23 Sep 2013, 03:36 AM
Hi Troika,

When you are using a CheckBoxList,which is inside a template column,you can access it only using findcontrol.Please try the following code.

ASPX:
<EditItemTemplate>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" ClientIDMode="Static">
    </asp:CheckBoxList>
</EditItemTemplate>

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
   {
     string values = string.Empty;
     if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           GridEditableItem edit = (GridEditableItem)e.Item;
           CheckBoxList check = (CheckBoxList)edit.FindControl("CheckBoxList1");        
           for (int i = 0; i < check.Items.Count; i++)
           {
               if (check.Items[i].Selected)
               {
                   values = check.Items[i].Text.ToString();
                   if (values == "Administradores")
                   {
                       check.Enabled = false;
                   }
               }
           }
       }
   }

Thanks,
Princy
0
Troika
Top achievements
Rank 1
answered on 23 Sep 2013, 10:09 AM
i still cant find it.

for exemple i'm using it on update command this to catch a simple texto on textbox

if (e.Item is GridEditableItem && e.Item.IsInEditMode)

{

GridEditFormItem data = e.Item as GridEditFormItem;


TextBox txt = (TextBox)data["Email"].Controls[0];

new_email = txt.Text;

}

so why for the checkboxlist, item databound it wont work at all


here is my code:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
 
 
   //  esconde user root
 
    if (e.Item is GridDataItem)
    {
 
        GridDataItem items = (GridDataItem)e.Item;
 
        string user = items["UserName"].Text;
 
        if (user == "root")
        {
 
            items.Display = false;
 
        }
 
    }
 
 
 
 
 
 
    foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
    {
 
        if (col.DataType == typeof(string))
        {
 
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //esconde as colunas[iguais do pre-render] em edit mode
 
                GridEditFormItem myColumn = (GridEditFormItem)e.Item;
                myColumn["Aproved"].Parent.Visible = false;
                myColumn["IsLockedOut"].Parent.Visible = false;
                myColumn["BloqSys"].Enabled = false//desactiva esta
 
 
 
 
               
 
 
                //transforma todos os campos de edit mode em textbox a exepcao dos abaixo indicados e os directamente
                //declarados em design mode  dentro da tag <columns>
 
                GridEditableItem item = (GridEditableItem)e.Item;
 
                TextBox txt = (TextBox)item[col.UniqueName].Controls[0];
             
 
                string value = txt.Text;
 
                txt.Visible = true;
 
             
                // checkboxlist roles
                if (col.UniqueName == "RoleName")
                {
 
                    CheckBoxList checks = new CheckBoxList();
 
                    checks.SelectedValue = value;
 
                    checks.DataSourceID = "sql_ds_funcao";
 
                    checks.DataTextField = "RoleName";
 
                    checks.DataValueField = "RoleName";
 
                    checks.ID = "CBLRole";
 
                    txt.Visible = false;
 
                    checks.ClientIDMode = ClientIDMode.Static;
 
                    item[col.UniqueName].Controls.Add(checks);
 
                }
 
                    //label
 
                else if ((col.UniqueName == "UserName") || (col.UniqueName == "CreationDate"))
                {
                    Label newlabel = new Label();
 
                    newlabel.Text = value;
 
                    newlabel.ID = "lnk" + Guid.NewGuid(); //Generating ID's for all label Controls
 
                    txt.Visible = false;
 
                    item[col.UniqueName].Controls.Add(newlabel);
                }
 
            }
 
        }
    }
}

i have deleted the code since it dont work....

also the code to hide user it working but for exemple we have in the grid a gray and then a white row and because i'm hidding one it creates 2 whites because the gray is hidden :S

0
Princy
Top achievements
Rank 1
answered on 24 Sep 2013, 04:47 AM
Hi Troika,

Please add the code to create the checkbox in ItemCreated Event of the radgrid.Please try the following code snippet.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
        {
            if (col.DataType == typeof(string))
            {
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    GridEditableItem item = (GridEditableItem)e.Item;
                    TextBox txt = (TextBox)item[col.UniqueName].Controls[0];//Access the textbox in editmode
                    string value = txt.Text;
                    if (col.UniqueName == "RoleName")
                    {                  
                        CheckBoxList checks = new CheckBoxList();                     
                        checks.DataSourceID = "SqlDataSource2";
                        checks.DataTextField = "RoleName";
                        checks.DataValueField = "RoleName";
                        checks.ID = "CBLRole";
                        checks.ClientIDMode = ClientIDMode.Static;                     
                        item[col.UniqueName].Controls.Add(checks);
                    }
                    else
                    {
                       // Your Code
                    }
                }
            }
        }
    }
  
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        string values = string.Empty;
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditFormItem data = e.Item as GridEditFormItem;
            CheckBoxList checkedItems = (CheckBoxList)data.FindControl("CBLRole");//Accessing the Template CheckBoxList
            
            for (int i = 0; i < checkedItems.Items.Count; i++)
            {
                if (checkedItems.Items[i].Selected)
                {
                    values = values + "," + checkedItems.Items[i].Text.ToString();//Storing the selected values
                }
            }
            string selectedvalue = values.Trim(',');
        }
    }


Thanks,
Princy
Tags
Grid
Asked by
Troika
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Troika
Top achievements
Rank 1
Share this question
or