Struggled with this for some time - Wanted to be able to switch Insert and\or Edit buttons on a TreeList control from Telerik at runtime, depending on what its DataItem or NestedLevel is. Here is the solution, using the control's server-side API:
protected void OnMyTreeListItemCreated(object sender, TreeListItemCreatedEventArgs e)
{
//APPROACH #1
if (e.Item.ItemType == TreeListItemType.AlternatingItem || e.Item.ItemType == TreeListItemType.Item)
{
TreeListDataItem item = e.Item as TreeListDataItem;
if (item != null)
{
int level = item.HierarchyIndex.NestedLevel;
//DO THE JOB
LinkButton btnInsert = item["EditCommandColumn"].FindControl("InsertButton_EditCommandColumn") as LinkButton;
LinkButton btnEdit = item["EditCommandColumn"].FindControl("EditButton_EditCommandColumn") as LinkButton;
if (btnInsert != null) { btnInsert.Visible = (level == 0); }
if (btnEdit != null) { btnEdit.Visible = (level > 0); }
}
}
//APPROACH #2
var editableItem = e.Item as TreeListEditableItem;
if (editableItem != null)
{
SomeMyType boo = editableItem.DataItem as SomeMyType;
if (boo!= null && boo.SomeProperty == "SomeValue")
{
//SAME DO THE JOB AS IN #1
}
}
}
}
protected void OnMyTreeListItemCreated(object sender, TreeListItemCreatedEventArgs e)
{
//APPROACH #1
if (e.Item.ItemType == TreeListItemType.AlternatingItem || e.Item.ItemType == TreeListItemType.Item)
{
TreeListDataItem item = e.Item as TreeListDataItem;
if (item != null)
{
int level = item.HierarchyIndex.NestedLevel;
//DO THE JOB
LinkButton btnInsert = item["EditCommandColumn"].FindControl("InsertButton_EditCommandColumn") as LinkButton;
LinkButton btnEdit = item["EditCommandColumn"].FindControl("EditButton_EditCommandColumn") as LinkButton;
if (btnInsert != null) { btnInsert.Visible = (level == 0); }
if (btnEdit != null) { btnEdit.Visible = (level > 0); }
}
}
//APPROACH #2
var editableItem = e.Item as TreeListEditableItem;
if (editableItem != null)
{
SomeMyType boo = editableItem.DataItem as SomeMyType;
if (boo!= null && boo.SomeProperty == "SomeValue")
{
//SAME DO THE JOB AS IN #1
}
}
}
}
No comments:
Post a Comment