I have a template definition as below:
| public class BoundTemplate : System.Web.UI.ITemplate |
| { |
| public ListItemType templateType; |
| public string columnName, dataType, timeZone, sortExpression; |
| public bool convertTZ; |
| public event CommandEventHandler OnSorting; |
| public BoundTemplate(ListItemType type, string colName, string sortExpression) |
| { |
| templateType = type; |
| this.columnName = colName; |
| this.sortExpression = sortExpression; |
| } |
| public BoundTemplate(ListItemType type, string colName, CommandEventHandler sortHandler, string sortExpression) |
| { |
| templateType = type; |
| this.columnName = colName; |
| this.OnSorting = sortHandler; |
| this.sortExpression = sortExpression; |
| } |
| public BoundTemplate(ListItemType type, string colName, string dataType, string timeZone, bool convertTZ) |
| { |
| templateType = type; |
| this.columnName = colName; |
| this.dataType = dataType; |
| this.timeZone = timeZone; |
| this.convertTZ = convertTZ; |
| } |
| public void InstantiateIn(Control container) |
| { |
| switch (templateType) |
| { |
| case ListItemType.Header: |
| if (OnSorting != null) |
| { |
| LinkButton lb = new LinkButton(); |
| lb.Text = columnName; |
| //lb.CommandName = "Sort"; |
| lb.Command += new CommandEventHandler(lb_Command); |
| lb.CommandArgument = sortExpression; |
| container.Controls.Add(lb); |
| } |
| else |
| { |
| Label lbl = new Label(); |
| lbl.Text = columnName; |
| container.Controls.Add(lbl); |
| } |
| break; |
| case ListItemType.Item: |
| case ListItemType.AlternatingItem: |
| Label dataLabel = new Label(); |
| dataLabel.Text = columnName; |
| container.Controls.Add(dataLabel); |
| dataLabel.DataBinding += new EventHandler(Item_DataBinding); |
| break; |
| case ListItemType.Footer: |
| break; |
| } |
| } |
| public void lb_Command(object sender, CommandEventArgs e) |
| { |
| if (OnSorting == null) |
| throw new Exception("Sort handler is not hooked up."); |
| OnSorting(sender, e); |
| } |
| } |
In the code behind I add the control to the grid dynamically as below:
| GridTemplateColumn tfield = new GridTemplateColumn(); |
| BoundTemplate headerTemplate = new BoundTemplate(ListItemType.Header, displayName, fieldName); |
| if (field.SelectSingleNode("@sortable") != null && field.SelectSingleNode("@sortable").Value == "true") |
| headerTemplate.OnSorting += new CommandEventHandler(SortGrid4TemplateFields); |
| tfield.HeaderTemplate = headerTemplate; |
| tfield.ItemTemplate = new BoundTemplate(ListItemType.Item, columnName, dataType, timeZone, convertTZ); |
| tfield.ItemStyle.HorizontalAlign = SetAlignment(alignment); |
| tfield.DataField = columnName; |
| tfield.UniqueName = fieldName; |
| //tfield.DataType = Type.GetType(dataType); |
| tfield.SortExpression = fieldName; |
| //tfield.FilterListOptions = GridFilterListOptions.VaryByDataType; |
| RadGrid1.MasterTableView.Columns.Add(tfield); |
And finally, i have set the grid properties as below:
| <telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="true" AllowSorting="true" AllowPaging="true" PageSize="10" OnSortCommand="CustomSort"> |
| <MasterTableView AutoGenerateColumns="false" AllowCustomSorting="true"></MasterTableView> </telerik:RadGrid> |
The issue is the OnSorting event handler is never getting invoked, nor is the CustomSort handler. I am trying to integrate the RadGrid control into our existing grid which has like ten templates defined and this is one of them