How can I disable insert on an item by item basis?

4 posts, 1 answers
  1. David
    David avatar
    3 posts
    Member since:
    Jun 2008

    Posted 22 Mar 2011 Link to this post

    Hello,

    I'm using the automatically generated edit and insert controls for the RadTreeView and so far everything is working out fine, except that I'm stuck on one thing.

    I would like to limit the number of nested inserts a user can do by disabling and hiding the insert on certain rows (which will be determined programatically in the ItemDataBound Event).

    This is just a simple example of what I'd like to do.  Basically if the "level" of the item is 4, I would like to disable and hide the insert.

    protected void ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
       {
           if (e.Item.ItemType == TreeListItemType.AlternatingItem || e.Item.ItemType == TreeListItemType.Item)
           {
               TreeListDataItem item = e.Item as TreeListDataItem;
               int level = (int)DataBinder.Eval(item.DataItem,"Level");
               if (level == 4)
               {
                   item["InsertCommandColumn"].Enabled = false;
                    
               }
                 
           }
       }

    The item["InsertCommandColumn"].Enabled kind of works, but it keeps the control visible and confusing for the end user.

    If I hide the Cell entirely, all of the other cells shift over to fill the void.

    Is there a more graceful way to disable an automatically generated insert button on a row by row basis?
  2. Answer
    Martin
    Admin
    Martin avatar
    811 posts

    Posted 25 Mar 2011 Link to this post

    Hello David,

    Instead of hiding the cell that holds the buttons you can hide the button(s). Since you want to hide the buttons on a nested level basis you can use the ItemCreated event like this:

    protected void RadTreeList1_ItemCreated(object sender, Telerik.Web.UI.TreeListItemCreatedEventArgs e)
    {
        if (e.Item.ItemType == TreeListItemType.AlternatingItem || e.Item.ItemType == TreeListItemType.Item)
        {
            TreeListDataItem item = e.Item as TreeListDataItem;
            int level = item.HierarchyIndex.NestedLevel;
            if (level == 0)
            {
                (item["MyEditCommandColumn"].FindControl("InsertButton_MyEditCommandColumn") as LinkButton).Visible = false;
            }
            if (level == 1)
            {
                (item["MyEditCommandColumn"].FindControl("EditButton_MyEditCommandColumn") as LinkButton).Visible = false;
            }
        }
    }

    I hope this helps.

    Greetings,
    Martin
    the Telerik team
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. David
    David avatar
    3 posts
    Member since:
    Jun 2008

    Posted 25 Mar 2011 Link to this post

    Your example pointed me in the right direction, thanks!

  5. David
    David avatar
    3 posts
    Member since:
    Jun 2008

    Posted 25 Mar 2011 Link to this post

    Quick follow up question to anyone needing this code.

    I was losing my formatting when I opened a row for editing, so I had to also check the SelectedItem flag when determining if I should format the row.
Back to Top