I'm currently working on a web site reporting system, using the RadGrid. The system must be able to support multiple ReportTypes which return different number of columns, column names, and even hierarchy. I've setup a system that is driven by xml files and there are virtually no hard coded values, column names, templates, and so forth. It needs to be lightweight and dynamic. Most of the RadGrid settings are pulled in at the Page_Init event. The application is dynamically buidling columns with AutoGenerateColumns=true.
Everything is working great to this point but, I've run into a problem which I hope some of you can help me with. I dynamically create image icons and link to javascript at the column header level and also at the row level. Works great until a user initiaites some event that raises a PostBack at the hierarchy's child level. My custom controls added to the RadGrid are lost in the PostBack. Obviously I am doing something wrong but, I'm not sure how to address this.
In the code below you'll see that I am creating controls and adding them to the RadGrid. gridDataItem[gridColumn.UniqueName].Controls.Add controls collection. These are the controls which get lost in the PostBack.
Thanks!
Stacy
Everything is working great to this point but, I've run into a problem which I hope some of you can help me with. I dynamically create image icons and link to javascript at the column header level and also at the row level. Works great until a user initiaites some event that raises a PostBack at the hierarchy's child level. My custom controls added to the RadGrid are lost in the PostBack. Obviously I am doing something wrong but, I'm not sure how to address this.
In the code below you'll see that I am creating controls and adding them to the RadGrid. gridDataItem[gridColumn.UniqueName].Controls.Add controls collection. These are the controls which get lost in the PostBack.
<telerik:RadAjaxPanel ID="ParentRadAjaxPanel" runat="server"> |
<telerik:RadGrid ID="ParentRadGrid" runat="server" EnableEmbeddedSkins="false" Skin="BIC3RadSkin" |
OnItemDataBound="RadGrid_ItemDataBound" OnColumnCreated="RadGrid_ColumnCreated" |
OnNeedDataSource="RadGrid_NeedDataSource" OnSortCommand="RadGrid_SortCommand"> |
<MasterTableView> |
</MasterTableView> |
</telerik:RadGrid> |
</telerik:RadAjaxPanel> |
protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs newGridDataItem) |
{ |
try |
{ |
RadGrid radGrid = (RadGrid)sender; |
#region Create Custom Header |
if (newGridDataItem.Item.ItemType == GridItemType.Header) |
{ |
#region Create links to charts |
... |
// Box a the ItemType as it is easier to use |
GridHeaderItem gridHeaderItem = (GridHeaderItem)newGridDataItem.Item; |
// Iterate thorugh every column and format the value |
foreach (GridColumn gridColumn in radGrid.MasterTableView.RenderColumns) |
{ |
if (gridColumn.ColumnType != "GridExpandColumn" && gridColumn.ColumnType != "GridRowIndicatorColumn") |
{ |
... |
try |
{ |
// Create a LinkButton control instead of setting the columns' .text property or the javascript will not work |
LinkButton linkButton = (LinkButton)gridHeaderItem[gridColumn.UniqueName].Controls[0]; |
linkButton.Text = gridColumn.UniqueName; |
// Create a hyperLink control for the chart to work and does not interfere with the sort LinkButton |
if (_report.Grids[radGrid.ID][gridColumn.UniqueName].ChartColumnLink == true) |
{ |
String chartParameters = String.Format(" <a alt=\"Click to view chart\" href=\"javascript:ViewChart('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}');\"><img border=\"0\" src=\"images/iconChart.gif\"></a>", _report.ManagerUserID.ToString(), _report.DepartmentID, Convert.ToByte(_report.DepartmentDrillDown), _report.Location, Convert.ToByte(_report.ShiftWindow), _report.StartDate.ToShortDateString(), _report.EndDate.ToShortDateString(), _report.ReportType, _report.AggregationPeriod, gridColumn.UniqueName); |
HyperLink chartHyperLink = new HyperLink(); |
chartHyperLink.Text = chartParameters; |
gridHeaderItem[gridColumn.UniqueName].Controls.Add(chartHyperLink); |
} |
} |
catch |
{ |
// If this fails, allow the column name to go w/o the chart link by default |
} |
} |
} |
} |
#endregion Create links to charts |
} |
#endregion Create Custom Header |
... // Create Custom Footer ommitted for easier troubleshooting |
#region Put the proper formatting on values before display |
if ((newGridDataItem.Item is GridDataItem) == true) |
{ |
... |
GridDataItem gridDataItem = (GridDataItem)newGridDataItem.Item; |
// Iterate thorugh every column and format the value |
foreach (GridColumn gridColumn in radGrid.MasterTableView.RenderColumns) |
{ |
if (_report.Grids[radGrid.ID].ContainsKey(gridColumn.UniqueName) == true) |
{ |
// Create the grid data item text in a control or .text property |
Label textLabel = new Label(); |
if (gridDataItem[gridColumn.UniqueName].Text == "NaN") |
{ |
// Weird: Add both text and a control. If there are more controls added below, the text is hidden but, if there are no other controls, the text will show. |
textLabel.Text = RadGrid_FormatText(radGrid, gridColumn.UniqueName, "0"); |
gridDataItem[gridColumn.UniqueName].Text = RadGrid_FormatText(radGrid, gridColumn.UniqueName, "0"); |
} |
else |
{ |
// Do not display items that are special case exceptions governed by the .ReplaceHierarchyParentText property from xml |
if (String.IsNullOrEmpty(_report.Grids[radGrid.ID][gridColumn.UniqueName].ReplaceHierarchyParentText) == false && gridDataItem.OwnerTableView.ParentItem == null) |
{ |
textLabel.Text = _report.Grids[radGrid.ID][gridColumn.UniqueName].ReplaceHierarchyParentText; |
gridDataItem[gridColumn.UniqueName].Text = _report.Grids[radGrid.ID][gridColumn.UniqueName].ReplaceHierarchyParentText; |
} |
else |
{ |
// Weird: Add both text and a control. If there are more controls added below, the text is hidden but, if there are no other controls, the text will show. |
textLabel.Text = RadGrid_FormatText(radGrid, gridColumn.UniqueName, gridDataItem[gridColumn.UniqueName].Text); |
gridDataItem[gridColumn.UniqueName].Text = RadGrid_FormatText(radGrid, gridColumn.UniqueName, gridDataItem[gridColumn.UniqueName].Text); |
} |
} |
gridDataItem[gridColumn.UniqueName].Controls.Add(textLabel); |
#region Create a hyperLink control for the chart to work and does not interfere with the sort |
// Create a hyperLink control for the chart to work and does not interfere with the sort LinkButton |
if (_report.Grids[radGrid.ID][gridColumn.UniqueName].ChartRowLink == true) |
{ |
String chartParameters = String.Format(" <a alt=\"Click to view chart\" href=\"javascript:ViewChart('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}');\"><img border=\"0\" src=\"images/iconChart.gif\"></a>", gridDataItem[_childRadGridProperties["MasterTableView.DataKeyNames"]].Text, _report.DepartmentID, Convert.ToByte(_report.DepartmentDrillDown), _report.Location, Convert.ToByte(_report.ShiftWindow), _report.StartDate.ToShortDateString(), _report.EndDate.ToShortDateString(), _report.ReportType, _report.AggregationPeriod, gridColumn.UniqueName); |
HyperLink chartHyperLink = new HyperLink(); |
chartHyperLink.Text = chartParameters; |
gridDataItem[gridColumn.UniqueName].Controls.Add(chartHyperLink); |
} |
#endregion Create a hyperLink control for the chart to work and does not interfere with the sort |
} |
} |
} |
#endregion Put the proper formatting on values before display |
} |
catch (Exception exception) |
{ |
//UNDONE: Wireup error handler object |
Console.Write("error"); |
} |
} |
Thanks!
Stacy