4 Answers, 1 is accepted
I tried following code in order to expand/collapse button in groups header.
CS:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridGroupHeaderItem) |
{ |
GridGroupHeaderItem GroupHeader = (GridGroupHeaderItem)e.Item; |
Button btn = new Button(); |
btn.Text = "Expand/Collapse"; |
btn.Click += new EventHandler(btn_Click); |
GroupHeader.DataCell.Controls.Add(btn); |
} |
} |
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridGroupHeaderItem) |
{ |
GridGroupHeaderItem GroupHeader = (GridGroupHeaderItem)e.Item; |
Button btn = new Button(); |
btn.Text = "Expand/Collapse"; |
GroupHeader.DataCell.Controls.Add(btn); |
} |
} |
void btn_Click(object sender, EventArgs e) |
{ |
Button btn = (Button)sender; |
GridGroupHeaderItem GroupHeader = (GridGroupHeaderItem) btn.NamingContainer; |
GroupHeader.Expanded = !GroupHeader.Expanded; |
} |
-Shinu.
I'm sorry, I should have proof-read my original post. The functionality I'm looking for, which is implemented in the example referred to in that post for a hierarchical grid, is an Expand/Collapse All button in the Grid Header. In my original post, I incorrectly said "Group Header". Specifically in the attached image, I would like a button in the cell to the left of the header cell that reads "Manager", which when clicked would expand all groups or collapse all groups.
Note: I have already added code that creates that button in the RadGrid's PreRender event handler and it looks good, however I am unable to make use of the button as it is created after the page_load method and I believe I am unable to dynamically create a button at that point in the page refresh and have it fire. So when I click that button, the Page_Load event fires, but not the event that is attached to the button's OnClick method itself.
See my code below:
Button toggle = new Button(); |
toggle.ID = "ButtonExpandCollapse"; |
toggle.CssClass = "rgExpand"; |
toggle.CommandName = "ExpandAll"; |
toggle.UseSubmitBehavior = true; |
toggle.Click += new EventHandler(toggle_Click); |
toggle.EnableViewState = true; |
cell.Controls.Add(toggle); |
AjaxSetting ajaxsetting = new AjaxSetting(toggle.ID); |
ajaxsetting.UpdatedControls.Add(new AjaxUpdatedControl(RadGridRealMcCoy.ID, RadAjaxLoadingPanelRealMCoy.ID)); |
RadAjaxManagerRealMcCoy.AjaxSettings.Add(ajaxsetting); |
It would be sufficient if when the Page_Load event fires I could determine which control caused it to fire, and handle it accordingly, but I don't see where that's possible. Also, I could probably get away with placing an expand/collapse all button somewhere on the form, but I think the intuitive place for it to be is in the upper left hand corner of the grid itself.
Thanks, Steve
For the Click event of the button to get triggered try adding the button in the ItemCreated event rather than using the PreRender event. Check out the following code:
c#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridHeaderItem) |
{ |
GridHeaderItem header = (GridHeaderItem)e.Item; |
Button toggle = new Button(); |
toggle.ID = "ButtonExpandCollapse"; |
toggle.CssClass = "rgExpand"; |
toggle.UseSubmitBehavior = true; |
toggle.Click += new EventHandler(toggle_Click); |
toggle.EnableViewState = true; |
header["ExpandColumn"].Controls.Add(toggle); |
} |
} |
void toggle_Click(object sender, EventArgs e) |
{ |
RadGrid1.MasterTableView.HierarchyDefaultExpanded = !RadGrid1.MasterTableView.HierarchyDefaultExpanded; |
RadGrid1.Rebind(); |
} |
Happy New Year!
-Princy.
Thanks